Script: Update Linode DNS Records with WAN IPs (Dynamic DNS)
Either of these scripts will grab both the IPv4 and IPv6 (if any) addresses assigned to any WAN I’m behind, and, using Linode’s DNS API, will update my DNS records with same and log changes/errors using logger. In effect, it’s a homemade Dynamic DNS updater. Linode’s developing a new API so that’s why two versions exist.
To use the script, you need:
- A Linode API key (for version 3 of Linode’s DNS API) or Personal Access Token (for version 4 of Linode’s DNS API),
- the domain ID, and
- the resource (called record in v4) IDs of the DNS records you want to update.
The IDs don’t change, whether you’re using version 3 or version 4.
Since I use a Macbook Pro, the scripts use terminal-notifier (installed as a gem, see script coments on how to install) to display screen notifications. The scripts should port nicely to Linux by removing the terminal-notifier code.
I use the scripts to whitelist my WAN IPs for two things: to ensure my server’s firewall, ConfigServer Security & Firewall (csf), doesn’t accidentally lock me out, and to access protected directories via nginx. I created a cron job that runs every fifteen minutes and I’ve set my DNS records with TTLs of 300 (5 minutes).
I’ve noticed when travelling that some WANs have no IPv6 addresses at all. In this case, edit the script to set CHECK_IP6=0 otherwise you’ll get a lot of error notifications.
A few things I learned:
- When constructing a complicated URL for submission via curl, it’s best to use –data option instead of -d. It will save you some troubleshooting headaches.
- It’s not necessary to enclose the –data values in single quotes as shown in many examples. The single quotes won’t allow for expansion and that’s usually where you want to expand variables.
- Using a function to generate parts of the URL can be a lifesaver and save you from descending into escape hell.