This blog is hosted on a small, Ubuntu Linux-based server in my home. Given that we have a typical, residential Internet connection, our router’s IP address is subject to change at any time (this can make it challenging to provide you, the reader, reliable access to blog.chrisjscott.net).
My domain was, until recently, managed by Google’s domain services – one of the great parts about that service was that it provided a way to “monitor” my home’s IP address and automatically update the associated DNS record whenever the IP address changed – a process known as Dynamic DNS (DDNS). Unfortunately, Google – in typical Google fashion – recently killed their domain management service and moved me to Squarespace.
Squarespace doesn’t offer DDNS service, so I now have to manage this on my own. While I could utilize a third-party service to do this, most require a subscription to manage a permanent domain (rather then just a subdomain of the service provider). Given that I’m lucky enough that my home IP address rarely changes, I decided to forego yet another subscription cost and, instead, to whip up a shell script that detects when my IP address changes and to send me a text to let me know when I need to update my DNS’ ‘A’ record.
It’s pretty simple really – I’m taking advantage of two external tools here:
- https://www.ipify.org/ is a site that does nothing but display your current, external-facing IP address in plain text
- https://textbelt.com/ is an amazing, free service that enables you to send text messages to a specified phone number via shell script (the developer, Ian Webster, is very generous with his time and I encourage you to throw him a few dollars to support his efforts)
With those two things in-hand, here’s the script:
#!/bin/sh
# acquire IP address specified in current A record
dnsIP=$(dig chrisjscott.net A +short)
if [ "$(curl https://api.ipify.org/)" != "$dnsIP" ]; then
# textbelt.com doesn't permit passing of URLs or IP addresses, so I edit the value of the dnsIP variable to replace the IP addresses periods with colons
dnsIP=$(echo $dnsIP | sed -e "s/\./:/g")
curl -X POST https://textbelt.com/text \
--data-urlencode phone='+11234567890' \
--data-urlencode message="Update chrisjscott A record to $dnsIP." \
-d key=<yourTextbeltKey>
fi
# check Textbelt msg quota by appending _test to the end of the API key
Obviously, replace the blue text with your own values (you can generate you own Textbelt key by visiting their website)…
With this in-hand, I configure this to run on my Linux server as a cronjob every six hours and, should the IP address change, I get a text message notifying me that it’s time to visit Squarespace and update the record:
Update
You’ll note that I wrote this to ensure I wasn’t passing any URLs or IP addresses through Textbelt (since they don’t allow it). But then I realized that, during testing, when I did try to pass a URL, I got a warning back that let me know that, if I wanted to pass URLs, I could request that my API key be added to a whitelist!
Once I realized that (and got the exception), I removed the search & replace line and added my DNS manager’s URL (punched through the is.gd URL shortener for brevity) to the notification so that I could get to it directly from the text message I receive!
Leave a Reply