OpenWrt Forum Archive

Topic: dyndns update script

The content of this topic has been archived on 15 Feb 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

I wrote this because my router is NAT'd behind another (wifi chain, sharing comcast with a neighbor), so the router it is running on is static but the actual public IP is not.  Not sure how lightweight it is compared to the other clients (488kb RSS on my router), but it does the job for me.  I run it out of /etc/firewall.user

It also has a simple watchdog that watches the upstream IP and reboots in case the wifi link is flaky; not sure how helpful this is, it's definitely not very elegant.

root@Monkey:/etc# cat /etc/services_daemon.sh
#!/bin/ash
PATH=/bin:/usr/bin:/sbin

# domain name to update, and the url used to update it
domain_name=example.org
update_url="http://freedns.afraid.org/dynamic/update.php?XXXXXXXX"

# force dns server to check (optional)
dns_server=69.245.247.164

# How often to check for IP changes (in seconds)
refresh_time=30

# Router to ping to force reboot (watchdog)
# comment this out to skip watchdog
#watchdog_ip=192.168.1.1

#logfile=/jffs/services_daemon.log  # keeps a permanent log
logfile=/tmp/services_daemon.log   # loses the log on reboot
#logfile=/dev/tty                   # logs to console

pidfile=/var/run/services_daemon.pid

trap 'echo "Quitting..." >> $logfile ; rm $pidfile ; kill $$' 2

date +"Services Daemon starting up as PID $$ at %D at %T" >> $logfile

[ -f $pidfile ] && pid=`cat $pidfile` && {
   if [ -f /proc/$pid/status ] ; then
      echo "Services Daemon already running ($pid)"
      exit
   else
      echo "Stale PID ($pid) file found - possible crash?"
   fi
}
echo $$ > $pidfile

while true ; do
   [ $watchdog_ip ] && {
      ping -c 1 $watchdog_ip > /dev/null
      [ $? -gt 0 ] && {
         date +"Router missed ping on %D at %T" >> $logfile
         sleep 60
         ping -c 1 $watchdog_ip > /dev/null
         [ $? -gt 0 ] && {
            date +"Router missed ping again, rebooting on %D at %T" >> $logfile
            reboot
   }  }  }

   ip_dns=$(nslookup $domain_name $dns_server |tail -n 1|cut -f3 -d\ )

#   ip_now=$(wget -q -O - http://whatismyip.org) # this will not append a cr, and is slower
   ip_now=$(wget -q -O - http://checkip.dyndns.org|sed s/[^0-9.]//g)

   [ "$ip_now" == "$ip_dns" ] && [ $dns_has_not_pushed ] && {
      date +"DNS pushed update on %D at %T" >> $logfile
      dns_has_not_pushed=0
   }
   [ "$ip_now" != "$ip_dns" ] && [ "$ip_now" != "$ip_lastupdated" ] && {
      ip_lastupdated=$ip_now
      dns_has_not_pushed=1
      wget -q -O /dev/null $update_url
      date +"DNS updated to $ip_now on %D at %T" >> $logfile
   }
   sleep $refresh_time
done

There is a ddns-scripts package...

The discussion might have continued from here.