Skip to content

Instantly share code, notes, and snippets.

@tcarrondo
Last active March 24, 2019 11:10
Show Gist options
  • Save tcarrondo/e0127a195c55c44fb744b2169ba2dcaf to your computer and use it in GitHub Desktop.
Save tcarrondo/e0127a195c55c44fb744b2169ba2dcaf to your computer and use it in GitHub Desktop.
Auto update a Cloudflare DNS record with your public IP (DDNS)
#!/bin/sh
#
# Made with several bits from the same amount of other scripts...
# ping me if you need: Tiago Carrondo <tcarrondo@ubuntu.com>
#
# cron this file every 5 min or so...
# */5 * * * * /root/ddns_with_cloudflare.sh 2>&1 | /usr/bin/logger -t ddns_with_cloudflare
# account credentials
CLOUDFLARE_EMAIL='<cloudflare e-mail address>'
CLOUDFLARE_API_KEY='<api key>'
# zone information
DNS_ZONE_ID='<get this value via web panel>'
# record information
DNS_RECORD_ID='<get this value with the code below>'
DNS_RECORD_NAME='<fqdn of the ddns destination>'
DNS_RECORD_TYPE='A'
# public ip information
NEW_IP=`curl http://ifconfig.me/ip`
CURRENT_IP=`cat /var/tmp/current_ip.txt`
# Uncomment the lines below to get the DNS_RECORD_NAME value
# curl -cs -X GET "https://api.cloudflare.com/client/v4/zones/$DNS_ZONE_ID/dns_records?type=A&name=$DNS_RECORD_NAME" \" \
# -H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
# -H "X-Auth-Key: $CLOUDFLARE_API_KEY"
touch /var/tmp/current_ip.txt
if [ "$NEW_IP" = "$CURRENT_IP" ]
then
echo "No Change in IP Adddress"
else
curl -X PUT "https://api.cloudflare.com/client/v4/zones/$DNS_ZONE_ID/dns_records/$DNS_RECORD_ID" \
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
-H "X-Auth-Key: $CLOUDFLARE_API_KEY" \
-H "Content-Type: application/json" \
--data "{\"type\":\"$DNS_RECORD_TYPE\",\"name\":\"$DNS_RECORD_NAME\",\"content\":\"$NEW_IP\"}"
echo $NEW_IP > /var/tmp/current_ip.txt
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment