RouterOS script to invoke DDNS

One of the nice features of pfSense is that it includes an applet that updates your chosen Dynamic DNS (DDNS) provider with the current external IP of your network. Mikrotik has an experimental module that is supposed to update BIND servers that have dynamic updates turned on, but no DDNS client, leaving those who need this feature to use RouterOS’s scripting engine to get the job done.

To say that the scripting engine in RouterOS was powerful would be stretching reality a bit too far. I’d say instead that it’s “serviceable” in a non-programmer kind of way. As far as I’ve been able to determine it doesn’t have a lot of things you’d want in a programming environment. It doesn’t have regular expressions, for example. It does have loops and conditionals, as well as the ability to launch router commands. That puts it on a par with DOS or Windows shell scripting, mostly (you can install perl or python on Windows, where you can extend RouterOS in that way).

The end result of my efforts was a script that worked with my domain registrar. I named the file vpn-ddns.rsc as the url that I was most interested in keeping current was my VPN entry point.

# Set local variables. If you want to update subdomain.example.com,
# put in "subdomain" under subdomain and "example.com" in the domain
# variable. If you want to update just example.com with no subdomain,
# enter in "@" for the subdomain.

:local password "9e8tj83lknA7..."
:local subdomain "vpn"
:local domain "example.com"

# Get Public IP
/tool fetch url="http://bot.whatismyipaddress.com/" mode=http dst-path=pubIP.txt
:local currentIP [/file get pubIP.txt contents]
:log info "Current Public IP is:$currentIP"

# Update provider's DDNS
:local adminurl "https://dynamicdns.park-your-domain.com/update\3Fhost=$subdomain&domain=$domain&password=$password&ip=$currentIP"
/tool fetch url=($adminurl) mode=http dst-path=vpn-ddns.xml
:log info "DNS Successfully Updated"

This script, with some minor changes, is the same as that presented in a forum post entitled namecheap.com dynamic dns.

The primary changes I made were to replace the dummy data with my real Dynamic DNS account info, including the subdomain. I also defined the name of the file that fetch produces when it runs, naming it “vpn-ddns.xml” (the original code output an xml file named for the fetched url).

This entry was posted in System Administration on by .

About phil

My name is Phil Lembo. In my day job I’m an enterprise IT architect for a leading distribution and services company. The rest of my time I try to maintain a semi-normal family life in the suburbs of Raleigh, NC. E-mail me at philipATlembobrothersDOTcom. The opinions expressed here are entirely my own and not those of my employers, past, present or future (except where I quote others, who will need to accept responsibility for their own rants).