Dynamic DNS with Cloudflare

At home I use Ubiquiti gear for all of my networking and I use Cloudflare for my external DNS. Rather than use another service like DynDNS or No-IP, I set up a small script that runs on my EdgeRouter Lite that updates records for my stuff at home in a simple cron job. The script just uses Cloudflare’s API to update an existing record. I haven’t found a way to get the record name from the web interface yet so you do need to get the record ID from the API.

#!/bin/bash

key="your-api-key"
zoneID="your-zone-id"
email="you@yourmail.com"
recordID="record-id-to-update"
recordName="newrecord.yourdomain.com"
ip=$(ifconfig eth0 | grep "inet addr:" | cut -d: -f2 | awk '{ print $1 }')

curl -X PUT "https://api.cloudflare.com/client/v4/zones/$zoneID/dns_records/$recordID" \
     -H "X-Auth-Email: $email" \
     -H "X-Auth-Key: $key" \
     -H "Content-Type: application/json" \
     --data '{"type":"A","name":"'"$recordName"'","content":"'"$ip"'","ttl":120,"proxied":false}' -k

Another option rather than getting the IP from the interface is using icanhazip.com (thanks Major!) which will return a just your IP in a string. This way you don’t need to use your edge device to interface with Cloudflare, any internal system will work.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s