Dynamic DNS

I have finally set up my own dynamic DNS. I used to use a service for it, but it was complicated and I had to pay for it. So instead I just wrote the following bash script (special thanks to Mikey’s Lab for writing the original code, as well as my local LLM for helping me fix what wasn’t working on it). I am running this on my Linux machine and set up a cron job to run every 5 minutes.

The bash script:

#!/bin/bash

domain=$1
host=$2
APIKey=$3
APISecret=$4
WanIP=$(curl -s "https://api.ipify.org")
GDIP=$(curl -s -X GET -H "Authorization: sso-key ${APIKey}:${APISecret}" "https://api.godaddy.com/v1/domains/${domain}/records/A/${host}" | cut -d'[' -f 2 | cut -d']' -f 1 | jq -r '.data')
if [ "$WanIP" != "$GDIP" -a "$WanIP" != "" ]; then
        curl -s -X PUT "https://api.godaddy.com/v1/domains/${domain}/records/A/${host}" -H "Authorization: sso-key ${APIKey}:${APISecret}" -H "Content-Type: application/json" -d "[{\"data\":\"${WanIP}\"}]"
fi
ShellScript

Overview of the IP Update Process Using cURL and GoDaddy’s API

Retrieve Current WAN IP Address:

  • I use the curl command to fetch my public IP address and store it in a variable called WanIP.

Fetch Current A Record:

  • Next, I create a variable named GDIP (short for GoDaddy IP) to hold the current A record of my chosen domain.
  • To do this, I first authenticate using my GoDaddy API credentials.
  • Then, I make a GET request to GoDaddy’s API to retrieve the current A record for my domain.
  • I process the response to remove any square brackets ([ and ]) to ensure the data is JSON-compatible.

Compare IP Addresses:

  • I initiate an if statement to compare WanIP (my current WAN IP) with GDIP (the IP address from the A record).
  • If both IP addresses are the same, no further action is taken.
  • If they differ, I proceed to update the A record.

Update A Record:

  • Using another curl command, I send a PUT request to GoDaddy’s API with the correct IP address, ensuring that the A record is updated to match my current WAN IP.

Make it run automatically with crontab:

I type the following command:

crontab -e
ShellScript

Which will then ask me what editor I want to use (I use nano). It will then open the following:

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
*/5 * * * * /usr/local/bin/updateDNS.sh ianglenn.co www [yourapikey] [yourapisecret]
*/5 * * * * /usr/local/bin/updateDNS.sh ianglenn.co @ [yourapikey] [yourapisecret]
*/5 * * * * /usr/local/bin/updateDNS.sh ianglenn.co money [yourapikey] [yourapisecret]
*/5 * * * * /usr/local/bin/updateDNS.sh ianglenn.co pass [yourapikey] [yourapisecret]
*/5 * * * * /usr/local/bin/updateDNS.sh bellavittoria.com @ [yourapikey] [yourapisecret]
*/5 * * * * /usr/local/bin/updateDNS.sh bellavittoria.com new [yourapikey] [yourapisecret]
*/5 * * * * /usr/local/bin/updateDNS.sh bussyhub.com @ [yourapikey] [yourapisecret]
*/5 * * * * /usr/local/bin/updateDNS.sh jakspinrecords.com @ [yourapikey] [yourapisecret]
*/5 * * * * /usr/local/bin/updateDNS.sh thewokenpc.com @ [yourapikey] [yourapisecret]
*/5 * * * * /usr/local/bin/updateDNS.sh fabricsfindingsandfinishings.com @ [yourapikey] [yourapisecret]
ShellScript

After which I entered what I needed to run and what time (every 5 minutes) and voila! Home made dynamic DNS. I will write up a more in depth tutorial when I have the time.

References:

Original video that helped me write the script.