Automatically checks if your public IP has changed, and if so it verifies if your DDNS record has been updated accordingly.
This script is intended to run periodically (with a cronjob) to check if the public IP address of the machine has changed, and if so it checks if your custom DDNS record was updated accordingly. Without further customization it just logs these events under a file named log.txt
and keeps an history of the IP addresses under ip_history.txt
.
The first time the script runs it will create a file named lastrun_ip.txt
where it will store your current public IP address (retrieved with curl https://ipinfo.io/ip
). This file will be used in the next runs to check if there was an IP change.
Then at each next run the behavior will vary:
-
The public IP address didn't change since last time: an event of severity
INFO
is logged. -
The public IP address has changed, but the DDNS record was updated accordingly: the new IP address is saved with a timestamp in the file
ip_history.txt
and an event of severityWARN
is logged. You could add a custom action inside theif
block, e.g. to send you a notification or make an API call. -
The public IP address has changed, and the DDNS record doesn't match the new IP: an event of severity
CRIT
is logged. You could add a custom action inside theif
block, e.g. to update the record or send you a notification. -
The
curl
command fails (e.g. lack of connectivity): an event of severityCRIT
is logged. -
The
dig
command fails (e.g. wrong dns record): an event of severityCRIT
is logged.
You won't need much. Your system must be able to run dig
and curl
, which shouldn't be a problem on any distro. Your firewall (if any) should allow traffic to https://ipinfo.io
, used to retrieve your public IP address.
- Download ddnsupcheck.sh from the main branch to your local machine:
wget https://raw.githubusercontent.com/EarlyOwl/ddnsupcheck.sh/main/ddnsupcheck.sh
- Make it executable:
chmod +x ddnsupcheck.sh
- Open the script with your editor of choice:
nano ddnsupcheck.sh
- Edit the following variables to fit your needs:
#Your DDNS record to check
ddnsrecord="yourddnsrecord.example.com"
#Path for writing logs and other files. Default is current directory
workpath="."
#Timestamp used for logging. The default format is dd/mm/yyyy#h:m:s
timestamp=$(date +%d/%m/%y@%H:%M:%S)
- You can run the script manually with:
./ddnsupcheck.sh
But it would be better to set up a cronjob, for example to run each 5 minutes:
*/5 * * * * ./path/to/script/ddnsupcheck.sh
- (Optional) - Add some custom actions inside the
if
blocks of the script, e.g. sending a notification, updating the record, etc...
Yes and yes.
I'm eager to learn, open an issue or a pull request to suggest an improvement / fix.