Bash script to auto-update Dynamic DNS on FreeDNS

This bash script is especially useful when your router default dynamic dns function updates is not consistent and require reboot in order for it to update.

If you have a home machine that runs 24/7 connected to Internet, you can schedule a cronjob to execute this script on every 1 hour so your domain/subdomain always get updated with the latest dynamic IP.

#!/bin/bash

if test -f /root/freedns/freedns-last-ip.conf
then
	CacheIP=$(cat /root/freedns/freedns-last-ip.conf)
fi

CurreIP=$(wget http://freedns.afraid.org/dynamic/check.php -o /dev/null -O /dev/stdout | grep Detected | cut -d : -f 2 | cut -d '<' -f 1 | tr -d " ")

if [ "$CurreIP" = "$CacheIP" ]
then
	echo "Update not required..."
else
	echo "Updating http://free.afraid.org with " $CurreIP
	wget https://freedns.afraid.org/dynamic/update.php?insertyourapicode -o /dev/null -O /dev/stdout
	echo `date` "Updating log with IP " $CurreIP >> /root/freedns/freedns-upd.log
fi

rm -f /root/freedns/freedns-last-ip.conf
echo $CurreIP > /root/freedns/freedns-last-ip.conf

Leave a Reply

Your email address will not be published. Required fields are marked *