Skip to content

Instantly share code, notes, and snippets.

@tuldok89
Forked from Tras2/cloudflare-ddns-update.sh
Last active November 16, 2019 08:57
Show Gist options
  • Save tuldok89/4696ae8575576c2123cdadae2f8f4d46 to your computer and use it in GitHub Desktop.
Save tuldok89/4696ae8575576c2123cdadae2f8f4d46 to your computer and use it in GitHub Desktop.
A bash script to update a Cloudflare DNS AAAA record with the IPv6 address of the source machine
#!/bin/bash
# A bash script to update a Cloudflare DNS AAAA record with the IPv6 address of the source machine
# Used to provide DDNS service and IPv4 connectivity to my IPv6-only network.
# Needs the DNS record pre-creating on Cloudflare
# Proxy - uncomment and provide details if using a proxy
#export https_proxy=http://<proxyuser>:<proxypassword>@<proxyip>:<proxyport>
# Cloudflare zone is the zone which holds the record
zone=example.com
# dnsrecord is the A record which will be updated
dnsrecord=sub.example.com
## Cloudflare authentication details
## keep these private
cloudflare_auth_email=your@email.here
cloudflare_auth_key=0123456789abcdef0123456789abcdef
# Get the current global IPv6 address
ip=$(curl -s -X GET http://ipv6.icanhazip.com/)
echo "Current IP is $ip"
# if here, the dns record needs updating
# 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 "X-Auth-Email: $cloudflare_auth_email" \
-H "X-Auth-Key: $cloudflare_auth_key" \
-H "Content-Type: application/json" | jq -r '{"result"}[] | .[0] | .id')
echo "Zoneid 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=AAAA&name=$dnsrecord" \
-H "X-Auth-Email: $cloudflare_auth_email" \
-H "X-Auth-Key: $cloudflare_auth_key" \
-H "Content-Type: application/json" | jq -r '{"result"}[] | .[0] | .id')
echo "DNSrecordid for $dnsrecord is $dnsrecordid"
# update the record
curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records/$dnsrecordid" \
-H "X-Auth-Email: $cloudflare_auth_email" \
-H "X-Auth-Key: $cloudflare_auth_key" \
-H "Content-Type: application/json" \
--data "{\"type\":\"AAAA\",\"name\":\"$dnsrecord\",\"content\":\"$ip\",\"ttl\":1,\"proxied\":true}" | jq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment