Duck DNS Update Script for OpenWrt

Since Dyn stop offering free dynamic DNS, I have choose to use Duck DNS and I would like to share the following script I use to update Duck DNS free dynamic DNS hosted on Amazon EC2 on my OpenWrt Attitude Adjustment 12.09 running on TP-Link TL-WR1043ND v1. Please note that I am using wget in OpenWrt and the free dynamic DNS link have to change to HTTP from HTTPS.

#!/bin/sh

echo "# --------------------------------------------------------------------------"
echo "# Welcome to Duck DNS Update Script"
echo "# --------------------------------------------------------------------------"

# Change to your domain and token generated
domain="your-domain"
token="your-token"

log="/tmp/duckdns.log"
file="/tmp/ipaddress.duckdns"
DuckDNS="http://www.duckdns.org/update?domains="$domain"&token="$token"&ip="

if [ ! -e "$file" ]; then
echo "0.0.0.0" > $file
fi

CHECKIP=`wget -qO- http://icanhazip.com`
CURRENTIP=`cat $file`

if [ $CHECKIP != $CURRENTIP ]; then

echo "New IP $CHECKIP detected and updating current IP..."

wget -q $DuckDNS -O $log
sleep 10

status=`cat $log`
if [ "$status" == "KO" ]; then
echo "Failed to update Duck DNS..."
exit 1
fi

OLDIP=`head -1 $file`
echo $CHECKIP > $file
echo "Current IP $CHECKIP updated and old IP was $OLDIP..."

else

echo "No updated needed for $CHECKIP IP..."

fi

After the above script is save as /root/duckdns.sh you can proceed to add the following line at Scheduled Tasks in OpenWrt. You need to restart cron service.

*/5 * * * * /root/duckdns.sh &> /dev/null

Thank you.