Skip to content

Instantly share code, notes, and snippets.

@drizuid
Last active March 10, 2024 12:41
Show Gist options
  • Save drizuid/f22ba78c18e0fc0ae5219f585ae08e3c to your computer and use it in GitHub Desktop.
Save drizuid/f22ba78c18e0fc0ae5219f585ae08e3c to your computer and use it in GitHub Desktop.
cloudflare ddns
#!/bin/bash
# this is a simple script, it will curl to get your current ipv4 ip (change from ipv4 to ipv6 and A to AAAA to do this for ipv6)
# compare to what is set at cloudflare, and then update the record if needed. unlike most scripts you'll find, this does NOT
# use the global key. You need DNS::Edit for your api key.
# the record ID may not be needed, but this is how I set this up, maybe ill edit later
DOMAIN="<put the record you want to update here>"
NEWIP=$(host $DOMAIN| grep address | awk '{ print $4 }')
CURRIP=$(curl -s ipv4.icanhazip.com)
APIKEY="<put your api key with DNS::Edit here>"
ZONEID="<put your cloudflare domain's zone id here>"
RECORDID="<put the record id of the record you want to update here>"
TYPE="<the record type>"
if ! diff -q <( printf $NEWIP ) <( printf $CURRIP )>/dev/null 2>&1
then
echo "Different IP, updating..."
curl -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONEID/dns_records/$RECORDID" \
-H "Authorization: Bearer $APIKEY" \
-H "Content-Type: application/json" \
--data "{\"type\":\"A\",\"name\":\"$DOMAIN\",\"content\":\"$NEWIP\",\"ttl\":3600,\"proxied\":false}"
else
echo "No changes needed..."
fi
@drizuid
Copy link
Author

drizuid commented Nov 4, 2022

you can get record IDs by using
curl -X GET "https://api.cloudflare.com/client/v4/zones/<your zone>/dns_records?type=<the record type you want to see (like A or AAAA)>" -H "Authorization: Bearer <your token with at least DNS::READ>" -H "Content-Type:application/json"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment