ABSTRACT
A neighbor (looking at you Chad) recommended cloudflare for my website and I figured I’d try it out. Doing so entailed changing my NS records on my domain at the registrar. This effectively moved all my DNS zone for the domain over to cloudflare. Awesome! Getting the benefits of cloudflare, only thing is, I realized that my old bash script which kept my home network IP tied to a DNS host name now no longer works. I needed to update it. So I set out to rewrite the script and figured I’d share it here, hopefully to help someone else wanting to have a DDNS hostname for a dynamic IP at home. Without further delay, here’s the script, all that is needed is to plug in the values for the variable, set it in a crontab, and done:
#!/bin/bash
time=$(/bin/date)
myip=$(/usr/bin/curl -X GET "ipinfo.io/ip")
# Populate with your own cloudflare specs
myZoneID = ""
myRecordID = ""
myKey = ""
hostname = ""
email = ""
curl -X PUT "https://api.cloudflare.com/client/v4/zones/$myZoneID/dns_records/$myRecordID" \
-H "X-Auth-Email: $email" \
-H "X-Auth-Key: $myKey" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"'$hostname'","content":"'$myip'","ttl":1,"proxied":false}'
echo "$time IP Updated to: $myip for $hostname" >> /var/log/DNS-UPDATE-$(date +"%Y-%m-%d").log
In addition to updating your DNS record, this also keeps logs of the changes to preserve a history of IP address changes.