DDNS using the Cloudflare API
August 02, 2021
If you haven't come across Cloudflare before, read through my previous post for a rundown: What is Cloudflare?
Pre-requisites
- Domain Name
- Cloudflare Account (Free)
- Linux, Windows system
A bit about Dynamic DNS services
Dynamic DNS services allow you to maintain a host record with your IP address as it changes. For most users, this is one that can change frequently (on a lease). On the other hand if you have a static IP you can set up an A record within your DNS zone and call it a day.
Consumer routers supplied by ISPs have done this for some time now, but ultimately limited in the services they support Noteably, DynDNS or Netgear's own service. Third party firmware such as DDWRT or Tomato allow the user control to go as far as calling a unique web address, or running a script to update their IP.
Time to log into Cloudflare
If you're like me and you have a; Sonarr, Plex, Home Assistant or Transmission instance you host from your home network -- then Cloudflare does something that most other DDNS services can't... And that's masking your actual IP by "orange clouding" your record once it has been created.
First up, you'll need to obtain the your Cloudflare API Key.
- Log into Cloudflare > Go to your profile > Click on API Tokens
- Click on "View" next to Global API Key (this is your token value needed to execute the script)
Updating Cloudflare DNS entries using PowerShell
PowerShell is the tool of choice for most Wintel administrators (including me), it was only natural that I chose this method 😁
I've got the script hosted up on my GitHub repo, so let's go ahead and grab that.
git clone https://github.com/zenstrukt/cloudflare-ddns-updater.git
And finally, run the following to execute:
Import-Module Update-Cloudflare-DDNS.psm1
Update-CloudFlareDynamicDns -Token <#Your API Token#> -Email "YOUR_CLOUDFLARE_EMAIL" -Zone <#YOUR_DOMAIN#> -Record <#YOUR_HOST_RECORD#>
Wrapping up
Now... If that's all successful, you can now use Task Scheduler or Crontab to execute at any interval you choose.
Hope this has helped some of you.
