Created at: 30 Jan 2025 Last updated: 30 Jan 2025
export EDITOR=nano
nano ~/script_name.sh
#!/bin/bash
#
# script_name.sh - Updates the single A record for moab.jp in Cloudflare to match the current public IP.
# ========= USER CONFIGURATION =========
CF_TOKEN="YOUR_CLOUDFLARE_API_TOKEN_HERE"
ZONE_ID="YOUR_ZONE_ID_HERE" # The zone ID for moab.jp
RECORD_NAME="your.domain" # Or a subdomain like "home.your.domain
# =====================================
# 1) Get the current public IP
CURRENT_IP=$(curl -s ifconfig.me)
# 2) Find the record ID for the single A record named RECORD_NAME in this zone
LOOKUP_RESPONSE=$(curl -s -X GET \
"https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records?type=A&name=${RECORD_NAME}" \
-H "Authorization: Bearer ${CF_TOKEN}" \
-H "Content-Type: application/json")
# Extract the record ID from the JSON response (assuming exactly one result)
RECORD_ID=$(echo "$LOOKUP_RESPONSE" | /usr/bin/env python3 -c "
import sys, json
data = json.load(sys.stdin)
results = data.get('result', [])
print(results[0]['id'] if results else '')")
if [ -z "$RECORD_ID" ]; then
echo "Error: Could not find an A record for ${RECORD_NAME} in zone ${ZONE_ID}."
echo "Response was: $LOOKUP_RESPONSE"
exit 1
fi
# 3) Update that DNS record with the new IP
UPDATE_RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records/${RECORD_ID}" \
-H "Authorization: Bearer ${CF_TOKEN}" \
-H "Content-Type: application/json" \
--data "{
\"type\": \"A\",
\"name\": \"${RECORD_NAME}\",
\"content\": \"${CURRENT_IP}\",
\"ttl\": 120,
\"proxied\": true
}")
# 4) Print the Cloudflare update response
echo "Cloudflare update response: $UPDATE_RESPONSE"chmod +x script_name.sh
./script_name.sh
*/10 * * * * /Users/yourname/path/to/script_name.sh >> /Users/yourname/path/to/script_name.log 2>&1
# create the blog rails new blog # run the server in production with bindings sudo RAILS_ENV=production rails server -b your_local_ip -p 80