#!/bin/bash # zoneeditscript v0.7 # by sean@lolyco.com # # v0.7 quoted args to logger to stop it getting confused by GET_WAN_IP args # v0.6 a more rigorous check on result of GET_WAN_IP # v0.5 added ping -R method and 10 minute sleep for whatismyip.org # v0.4 added date to error output # v0,3 adds whatismyip.org # v0.2 moves wrongly placed sleep ZEDRELAX # # Run from eg. rc.local: # killall zoneeditscript; nohup /etc/zoneeditscript >> /var/log/zoneedit & # # Enter your Zoneedit username and password: ZEDUSER= ZEDPASS= ################################################################################# # choose a WAN IP discovery method - uncomment one only ################################################################################# # SNMP - change SNMP_IP to your SNMP-enabled router's LAN IP address # This option HAS CAUSED CHEAP ROUTERS TO BECOME UNRESPONSIVE! # SNMP_IP=192.168.1.1 # GET_WAN_IP="snmpwalk -v 1 -c public $SNMP_IP IP-MIB::ipAdEntAddr | grep -v $SNMP_IP | grep -v 127.0.0.1 | sed \"s/.* \([^ ]*\)$/\1/\"" # ZEDSLEEP=180 ################################################################################## # whatismyip.org - says once every ten minutes # GET_WAN_IP="curl -s http://www.whatismyip.org" # whatismyip.org says once every ten minutes! # ZEDSLEEP=600 ################################################################################# # whatismyip.com - hit slowly! # GET_WAN_IP="curl -s http://www.whatismyip.com/automation/n09230945.asp" # ZEDSLEEP=180 ################################################################################# # ping return path - set PING_HOST to the address or name of a nearby (friendly) host PING_HOST= # set WAN_IP_LINE to the number of the line your WAN address appears on WAN_IP_LINE=4 GET_WAN_IP="ping -nRc 1 $PING_HOST | sed -n ${WAN_IP_LINE}p | tr -d [:blank:]" # Assuming pings can be fairly frequent: ZEDSLEEP=60 ################################################################################# # zoneedit.com - you supply the code, I think they should change the page # GET_WAN_IP=# no zoneedit option, it's even more ugly than the SNMP one # ZEDSLEEP=180 ################################################################################ # In case of problems leading to repeated erroneous zoneedit update queries, # go to sleep for a long time after an update: # 1000 seconds is jut over a quarter of an hour. # ZEDRELAX=1000 # # Stop editing! # ZEDTHIS=$0 ZEDHOME=`dirname $0` IPADDR="" parse_hosts() { grep -v ^# $ZEDHOME/zoneedit.hosts | tr -d [:blank:] | grep .... | \ { while true do read host || break [ x$hosts == x ] && hosts=$host || hosts=$hosts,$host done echo $hosts } } update_zoneedit() { ZEDHOSTS=`parse_hosts`; ZEDRESP=`curl -s --user $ZEDUSER:$ZEDPASS http://dynamic.zoneedit.com/auth/dynamic.html?host=$ZEDHOSTS` if echo $ZEDRESP | grep ERROR then logger -t $ZEDTHIS ERROR on IP address update logger -t $ZEDTHIS "$ZEDRESP" elif echo $ZEDRESP | grep SUCCESS then logger -t $ZEDTHIS "Updated IP to $IPADDR" else logger -t $ZEDTHIS "Unexpected response: $ZEDRESP" fi sleep $ZEDRELAX } echo `date` Zoneedit restarted trap update_zoneedit SIGHUP IPOLDADDR="notset" while true do IPADDR=`eval $GET_WAN_IP` # check we got an IP address back, only 4 numbers, separated by 3 dots, no whitespace if [ x`echo $IPADDR | sed s/^[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$/IP/` == xIP ] then echo `date` $IPADDR [ x$IPADDR != x$IPOLDADDR ] && update_zoneedit IPOLDADDR=$IPADDR else logger -t $ZEDTHIS "No IP address in response from $GET_WAN_IP" logger -t $ZEDTHIS "IPADDR is $IPADDR" echo `date` No IP address: $GET_WAN_IP returns $IPADDR fi sleep $ZEDSLEEP done