Skip to content

Instantly share code, notes, and snippets.

@anisimovdk
Created August 15, 2024 12:09
Show Gist options
  • Save anisimovdk/a50563632c609887cf1be20a4baa0ebe to your computer and use it in GitHub Desktop.
Save anisimovdk/a50563632c609887cf1be20a4baa0ebe to your computer and use it in GitHub Desktop.
Cloudflare DynDNS Script for Synology NAS
### Cloudflare Dynamic DNS for Synology NAS
# 1. Open Synology Task Manager (Synology UI -> Control Panel -> Task Scheduler)
# 2. Create task, define schedule, notification, etc
# 5. Define this Bash Script and specify your zone, dnsrecord to updatte, and Cloudflare API Token (https://dash.cloudflare.com/profile/api-tokens)
#!/bin/bash
# Settings
zone=example.com
dnsrecord=abc.example.com
cloudflare_auth_token=12345678910QAZXSWEDCVFRTGB1234567890asdw
# Get the current external IP address
ip=$(curl -s -X GET https://checkip.amazonaws.com)
echo "Current IP is $ip"
if nslookup $dnsrecord 1.0.0.1 | awk -F': ' 'NR==6 { print $2 } ' | grep "$ip"; then
echo "$dnsrecord is currently set to $ip; no changes needed"
exit
fi
# Get the zone ID for the requested zone
zoneid=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone&status=active" \
-H "Authorization: Bearer ${cloudflare_auth_token}" \
-H "Content-Type: application/json" | grep -o '"id":"[^"]*' | grep -o '[^"]*$' | head -n 1)
echo "Zone ID for $zone is $zoneid"
# Get the DNS record ID
dnsrecordid=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records?type=A&name=$dnsrecord" \
-H "Authorization: Bearer ${cloudflare_auth_token}" \
-H "Content-Type: application/json" | grep -o '"id":"[^"]*' | grep -o '[^"]*$')
echo "DNS record ID for $dnsrecord is $dnsrecordid"
# Update the record
curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records/$dnsrecordid" \
-H "Authorization: Bearer ${cloudflare_auth_token}" \
-H "Content-Type: application/json" \
--data "{\"type\":\"A\",\"name\":\"$dnsrecord\",\"content\":\"$ip\",\"ttl\":1,\"proxied\":false}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment