HardwareScript

Custom DDNS on Ubiquiti USG

Been using Ubiquiti in the lab for more than a year now. Really like this simplicity of operations, but at the same time I have full access to the linux alike OS underneath. This gives you the possibility to run scripts and other tasks. Which brings me to what this blog post is all about. I have an ISP which charges extra for having a static IP address and me being lazy and cheap (Also it would be more than the monthly cap I have from work), I have not “upgraded” to a static IP. So I thought why not just use a DDNS (Dynamic DNS) service. The USG that I have supports a few of them, but they all had some constraints. So I sat out to build my own solution, after all how hard can it be to update a DNS entry now and then?

MYIP

So there are a few components which all needs to fit together. I need to know what me external IP address is. I have as long as I can remember always used www.myip.dk, to get that answer. So I looked at that service and found that it has an API. YEAH! The API can give you the IPv4 or IPv6 address. With that solved on to next problem, updating a DNS entry.

UnoEuro DNS

This website (MichaelRyom.dk) is hosted by UnoEuro together with the sites DNS records. Looking at UnoEuros DNS management, I noticed that it also had an API! YEAH 2 for 2! All I needed was my own API key, to be able to update DNS records. With that knowledge, building a simple URL with all the needed information was a piece of cake.

All that is needed

URL: https://api.unoeuro.com/ddns.php

API key: Which you get from control panel on UnoEuro.com

Domain: The domain you want to change DNS record for. fx. MichaelRyom.dk

Hostname: The subdomain which should have its entry updated. fx. vpn – ie vpn.MichaelRyom.dk

MyIP: The IP, the DNS entry needs to be updated with.

 

The script

If you have a Cloud key first SSH to it. Username should be “ubnt” and password is the password of the admin. From the Cloud key you can SSH to the USG, with the following command.

ssh <username>@<USG IP or DNS>

Once logged into the USG, you need to become root to make the changes.

sudo -i

For the config not to be overwritten doing upgrades a post config script is created which will in turn create the script as a cron job (scheduled task), that will update the DNS record once every hour. Change directory to /config/scripts/post-config.d.

cd /config/scripts/post-config.d

Next use vi/vim to create the file and populate it with the script data.

vi create-ddns

Use i or insert to be able to edit in vi/vim and paste the below into the file.

#!/bin/bash
cat << 'EOF' > /etc/cron.hourly/ddns
#!/bin/bash
IP=$(curl -s -L "http://ipv4.myip.dk/api/info/IPv4Address" | sed -e 's/^"//' -e 's/"$//')
curl -s -L "https://api.unoeuro.com/ddns.php?apikey=<API key>&domain=<Domain>&hostname=<Sub domain>&myip=$IP" | logger
EOF

chmod +x /etc/cron.hourly/ddns

Whenever the USG is rebooted the above script is executed and a cron job will be created (or overwritten). Notice the lines starting with “IP” and “curl”. The IP line is what is used to gather the USGs pubkic IPv4 address. The line starting with curl is used to update the DNS record at UnoEuro. At the end of the line it is piped to the command “logger”. Logger is used as a simple way to add an entry to the messages log file under /var/log/ – More on that later.

chmod +x /config/scripts/post-config.d/create-ddns

sh /config/scripts/post-config.d/create-ddns

sh /etc/cron.hourly/ddns

Now in order to make it all work, first the script is made executable and then the create-ddns script is executed manually and lastly the cron job is also executed manually. If you want to see that the create-ddns script is working without you manually executing the scripts, you can just reboot the USG and that should do the trick.

 

The end

To verify that the cron job has been running/executed check the /var/log/messages file.

root@USG:~# tail -F /var/log/messages
Oct 30 10:42:35 USG logger: nochg 8.9.10.11
or
root@USG:~# cat /var/log/messages | grep logger
Oct 30 10:42:35 USG logger: nochg 8.9.10.11

As it can be seen above, there is an entry and it states what has happened and what the IP is. “nochg” as it says, means that no changes has been done.

Hope you like it and can get it to good use! VPNing into my USG has just become a whole lots easier!

The script can also be found on github.

2 thoughts on “Custom DDNS on Ubiquiti USG

Leave a Reply

Your email address will not be published. Required fields are marked *