Using CloudFlare API for Dynamic DNS

The objective is client-less, automated dynamic DNS functionality via a regularly-executed script. Python is the language of choice to allow our script to be used on any system. An example Python script can be found here – save this code on your system as “cfu.py”, and proceed with these instructions.

You will also need a CloudFlare account with the domain already set up and an existing A record for the subdomain you want to use.

On Windows

For this setup we will need to install Python first, then set up a Task Scheduler entry. To install Python, open up a Command Prompt as Administrator and run:

Then:

Once Python is installed, place your script somewhere out of the way (C:\Windows\ or perhaps %username%\Scripts\ works well). Next we will set up a scheduled task.

Open up Task Scheduler and select your Task Scheduler Library on the left-hand side. Create a new folder and name it something unique. Inside we will create a task for our script by right-clicking the folder and hitting “Create Task…” (not basic).

Name your task CloudFlare DDNS (or similar). In the Security options box of this page, click “Change User or Group…”, type SYSTEM  and hit Check Names. OK that window and move to the Triggers tab.

Hit “New…” and select “At startup” on the dropdown. In the Advanced settings box, tick “Repeat task every:” and select “1 hour” for a duration of “Indefinitely”. Make sure it’s enabled and hit OK. Move to the Actions tab.

Hit “New…” and put the full path to your python.exe in the Program/script location (it should look like C:\Python27\python.exe ). In the Add arguments entry, put the full path to the script, then your subdomain and domain (should look like C:\Windows\cfu.py subdomain yourdomain.com ).

Hit OK to close the action, OK to close the new task and it will appear in the folder you created. Right-click and hit “Run”, then check your CloudFlare panel to make sure it works. You can set the A record to 192.168.0.1 manually in CloudFlare and then run the script to test.

On Linux

Save your script as “cfu.py” and make it executable:

Move it to your bin folder:

Check it’s now available in your system path:

Prepare your cron statement for hourly checking. It should look like:
0 * * * * cfu.py subdomain yourdomain.com
Replace ‘subdomain’ and ‘yourdomain.com’ and add it to your crontab:

Confirm your line has been added by typing:

On OSX

Save your script as “cfu.py” somewhere accessible, then open Terminal and navigate to its location. Make it executable:

Move it to your bin folder:

Now install LaunchControl from here and start it up.

Hit ⌘N to create a new job and label it local.ddns.

Replace the contents of the Program to run box with the following, replacing subdomain and yourdomain.com:

On the right-hand side of the app, find and drag StartInterval into the centre and input 3600 seconds.

Save the new plist with ⌘S and it should run immediately. If configured correctly, local.ddns should report a green “OK” in the left panel.