Guest User

CF_DDNS

a guest
Jul 20th, 2025
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #Cron it for every 5 minutes.
  2. */5 * * * * /opt/scripts/ddns_updater.sh
  3.  
  4. #!/bin/bash
  5.  
  6. # Set these variables
  7. ZONE_ID="XXXXXXX" #Find this under the API section of your root DNS
  8. DNS_ID="XXXXXX" #This one is tricky to find. Make an edit to the DNS you want to be dynamic > From Homepage > Manage Account > Audit Logs > Find the edit you made < Click the log > Grab the resource ID
  9. API_TOKEN="XXXXXXX"
  10. #To get your API token > Sign in to CF >Pick your Domain you want DDNS on > To your bottom right find API and click "Get API TOKEN" >Click Create API Token
  11. #Permission 1 needed ZONE > DNS Settings > Edit
  12. #Permission 2 needed ZONE > DNS > Edit
  13. #Include your zone (Root URL) > Save it and your API Key
  14. RECORD_NAME="external-link-url" #The domain or subdomain you want to update. I Cname everything to a primary point and then have that primary point be DDNS
  15. TIMESTAMP=$(date -R)
  16.  
  17. # File to store last known IP
  18. IP_FILE="/var/tmp/current_ip.txt"
  19.  
  20. # Get current public IP
  21. CURRENT_IP=$(curl -s https://api.ipify.org)
  22.  
  23. # Check if IP file exists and read last IP
  24. if [[ -f "$IP_FILE" ]]; then
  25. LAST_IP=$(cat "$IP_FILE")
  26. else
  27. LAST_IP=""
  28. fi
  29.  
  30. # Compare and update if changed
  31. if [[ "$CURRENT_IP" != "$LAST_IP" ]]; then
  32. echo "IP changed: $LAST_IP -> $CURRENT_IP"
  33. echo "$CURRENT_IP" > "$IP_FILE"
  34.  
  35. # Update Cloudflare DNS record
  36. curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$DNS_ID" \
  37. -H "Content-Type: application/json" \
  38. -H "Authorization: Bearer $API_TOKEN" \
  39. -d "{
  40. \"comment\": \"Updated on $TIMESTAMP\",
  41. \"content\": \"$CURRENT_IP\",
  42. \"name\": \"$RECORD_NAME\",
  43. \"proxied\": true,
  44. \"ttl\": 3600,
  45. \"type\": \"A\"
  46. }"
  47. fi
Advertisement
Add Comment
Please, Sign In to add comment