EdgeRouter - Custom Dynamic DNS with Cloudflare


Readers will learn how to setup Dynamic DNS via Cloudflare API to point to the EdgeRouter IP.

For background, I host DNS for my own domain with Cloudflare and wanted my own dynamic DNS setup. This assumes dynamic DNS is already configured and running, since we're hooking into it.

We will use the Cloudflare API to update an A record that will point to our router IP. We will modify the script that generates the ddclient config. If you feel uncomfortable with that, you could run the script we write below via cron instead.

1. Find the id of the DNS record in Cloudflare. Mine is an A record called 'home' that should point to my public IP. Run the following and find the record id in the JSON output. You get your API token in your Cloudflare Account settings.

curl https://www.cloudflare.com/api_json.html \
  -d 'a=rec_load_all' \
  -d 'tkn=your_token_here' \
  -d 'email=your_email_here' \
  -d 'z=your_domain_here'

 2. On the router, create a script in /config/scripts. I called mine update_cf.sh. Put this in it:

ip=`vbash -ic "show interfaces" | grep pppoe0 | awk '{print $2}'`
curl https://www.cloudflare.com/api_json.html \
-d 'a=rec_edit' \
-d 'tkn=your_token_here' \
-d 'email=your_email_here' \
-d 'z=your_domain_here' \
-d 'id=the_id_we_found_earlier' \
-d 'type=A' \
-d 'name=home' \
-d 'ttl=1' \
-d "content=$ip" \
> /dev/null

 You may have to adjust your ip=foo line to get the correct interface. Mine is PPPoE. Also, my record name is home. Whatever you put here will be the name after it updates. The rest of the script should be self explanatory. Don't forget to chmod a+x it.

You can run it manually without redirecting the curl output to /dev/null to see whether it generally works.

3. We need to run this everytime that ddclient runs, so you need to edit /opt/vyatta/sbin/vyatta-dynamic-dns.pl. That script generates the config file for ddclient. Around line 108 you'll see a bunch of '$output .= foo'. Add another line before the 'return $output;' statement that looks like this:

    $output .= "postscript=/config/scripts/update_cf.sh\n";

 That adds the postscript config to the ddclient conf file next time it's auto-generated. That will happen on reboot, though there may be another way; if there is, I don't know it, so reboot the router.

Voila, as ddclient runs, it updates your dynamic DNS provider, and then runs the script we wrote to also update the Cloudflare record, keeping it up to date with your dynamic IP address.

Note: If the Public IP does not need the CIDR notation you can use this script example rather than the one above to remove the /xx.
ip=`vbash -ic "show interfaces" | awk '/pppoe0/ {print substr($2,1,index($2,"/") - 1)}'`

In this example /pppoe0/ is used; substitute pppoe0 with your corresponding WAN interface.  

 and Ubiquiti Employee

Powered by Zendesk