Updating GoDaddy A Record to Dynamic IP with Crontab and Bash Script

all about tech.
Post Reply
george
Site Admin
Posts: 16
Joined: Thu Aug 01, 2019 3:16 am
Updating GoDaddy A Record to Dynamic IP with Crontab and Bash Script

Post by george » Sun Sep 20, 2020 10:39 am

1) Go to godaddy, login to the host account and generate an API key/secret: https://developer.godaddy.com/keys

2) Make sure there is an a record created like such
a record.png
a record.png (23.73 KiB) Viewed 34531 times

3) Go to the host server and type:

Code: Select all

cd /home/YOUR-USERNAME-HERE; nano ddns-a-update.sh
but replace YOUR-USERNAME-HERE with your server username

4) paste the following code into the nano text editor, and make sure to replace YOUR-DOMAIN-HERE, YOUR-API-KEY-HERE, and YOUR-API-SECRET-HERE with the correct website (without www.), api key and api secret (supplied from the godaddy website in step 1):

Code: Select all

#!/bin/bash
domain=YOUR-DOMAIN-HERE.com
host=@
APIKey=YOUR-API-KEY-HERE
APISecret=YOUR-API-SECRET-HERE

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`

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
5) Press Ctrl+O to save and then Ctrl+X to exit

6) Type:

Code: Select all

crontab -e
(select nano as the text editor, if asked, and make sure to not be logged in with root (sudo su - if you are type exit, then use crontab -e again without root permissions)

7) Below all the lines of comments, paste in the new crontab task:

Code: Select all

0 *    *   *   *          /home/YOUR-USERNAME-HERE/ddns-a-update.sh
but replace YOUR-USERNAME-HERE with your server username

This will execute every hour. Use https://crontab.guru/ to figure out your desired update formatting.

8) Press Ctrl+O to save and Ctrl+X to exit

Done! Now your GoDaddy A record will update dynamically every hour to your new IP address.


Post Reply