Subject: UpdateDHS script
To: None <netbsd-help@netbsd.org>
From: Pat Wendorf <beholder@unios.dhs.org>
List: netbsd-help
Date: 02/27/2001 11:47:28
This is a multi-part message in MIME format.
--------------47323E51604F9C7F77BD1D6A
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hey NetBSD'ers,

A while back I was having some trouble getting a reliable DHS (dynamic
hosting services http://www.dhs.org, they provide static and dynamic
domain names for free!) script to work reliably with my flakey dialup. I
have FreeBSD, NetBSD, OpenBSD and Linux boxes on my network and at times
I switch over the NAT functionality to one of the boxes when I'm
"monkeying" (I can't honestly say working, because that's productive ;)
with the main NAT box.  I originally used a fairly simple PERL script
someone wrote (happy camper) but, it needed too many libraries installed
to work.  This is not a problem with my bigger machine, but I didn't
want to waste space on my little 250M drives on my Mac's (plus it's time
consuming to download and install these libs).  So my search resumed and
eventually I found a very nice shell script written for bash/linux
only.  This script had some very cool features, but it hadn't been
updated in a long while, so I modified the hell out of it, and it now it
runs with the default sh found on any linux or bsd system, it has OS
detection to determine what it's running on and what http-post engine to
use (lynx, fetch, wget), and quite a few other features that I've even
forgotten myself.

The problem that I'm having with NetBSD is that there is that I can't
find a program in the base install that can do a http-post from a
command line.  With NetBSD I just default the script to use Lynx, but it
can be overridden to use wget.  Can anyone comment on this choice?  Is
there something in the base system that can be used to send a value to a
web page?

I'll attach the script in case anyone finds it useful.


-- 

Pat Wendorf
--------------47323E51604F9C7F77BD1D6A
Content-Type: text/plain; charset=us-ascii;
 name="updatedhs"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="updatedhs"

#!/bin/sh
#
#  updatedhs 0.2.0v
#  Pat Wendorf <beholder@unios.dhs.org>
#  http://beholder.2y.net
#
#  Orginaly work done by: 
#  Dan Aloni <dax@karrde.org>
#  http://ten.n3.net/updatedhs
#  Sat Nov 11 15:12:01 IST 2000, stardate [-4] 77865.6
#  __________________________________________________________________________
#
#  This script automatically updates your dynamic host IP on dhs.org.
#  If you don't know what is dhs.org, you might not need this script :-)
#  or you might want to check http://www.dhs.org.
#
#  You are free to modify and spread any part of this file at will.
#
#  The script uses wget, software for retrieving web pages or other 
#  files from web sites. You can get wget anywhere (www.freshmeat.net),
#  or you might already have it, so don't panic.
#
#  Before running you need to make sure that wget can access the Internet, or 
#  at least www.dhs.org.
#
#  NOTE: There is no checking done on the output of wget to see if the update
#  was successful
#
#  Run 'updatedhs --help' for help about usage.
#
#
#
#  Default Settings
#  __________________________________________________________________________
#
#  Information about your dhs hostname is needed by the script in
#  order for it to work. 
#
#  You can specify the default settings for your dynamic host and the script
#  will work according to them, unless you overwrite these from the command 
#  line. Personnaly, I think you should write all your settings here except 
#  your password, and then specify your password in the command line.
#

DHS_USER=				# Your dhs username 
DHS_PASSWD=				# Your dhs user password
DHS_HOST=	  	    		# Your dhs domain
DHS_DOMAIN=      	  		# dhs domain 
DHS_MX=      	  			# dhs domain MX 


#  For example, if your dhs username is blabla, and your password is foofoo
#  And your hostname is barbar.2y.net, your settings would be:
#
#  DHS_USER=barbar
#  DHS_PASSWD=foofoo
#  DHS_HOST=barbar
#  DHS_DOMAIN=2y.net
#
#
#
#  Dynamic IP acquiring
#  __________________________________________________________________________
#
#  Now the script only needs to know the IP you want to set your hostname
#  to. It can do it automatically by calling ifconfig on device ppp0, this
#  is the default:
  
IP_METHOD=ifconfig
IP_OS=default 	  # This can be Linux, FreeBSD, OpenBSD or NetBSD 
IP_DEVICE=default # Change this if you want to get the IP from a 
		  # specific device (default is ppp0 for Linux and NetBSD, tun0
		  # for FreeBSD and OpenBSD  which should work with 
		  # dialup ppp and PPPoE)

#  if you want to set your IP manually, then uncomment the next 2 lines:
#
#  IP_METHOD=manual 
#  IP_MANUAL=100.101.102.103  # or whatever the IP you want to set
#
#
#
#  Updating the server
#  __________________________________________________________________________
#
#  On the next release of updatedhs you would be able to set this one to 'lynx',
#  and it will use lynx instead of wget, for some people who can't seeem to get
#  wget working.

IP_SENDER=default		# wget is default for Linux, fetch for
				# FreeBSD, and lynx for OpenBSD and NetBSD
				# in which case it is the built-in fetch
				# command (code graciously provided by 
				# 		wim.livens@easynet.be)

#
#  Cached IP [OPTIONAL]
#  _________________________
#
#  To have updatedhs only update the server if the IP has changed:
#  Note: You *want* this.
#

DHS_IPCACHE=yes

#
#
#  Cache the IP on disk [OPTIONAL]
#  _________________________
#
#  Let it store the IP of each host in a cache file 
#  (an idea by ravton <ravton@yahoo.com>),
#  to prevent unnecessary server updates if you restart updatedhs:

DHS_IPCACHE_DIR=/tmp

#
#
#  Daemon mode [OPTIONAL]
#  __________________________________________________________________________
# 
#  If you set DHS_DAEMONIZE to a certain number of seconds, updatedhs will
#  not stop running until you ^C, and between sleeps for that amount of time
#  it checks if the IP was changed and updates the server if it was.
#  
#  If you didn't specify DHS_IPCACHE=yes, it will automatically turn it on
#  so, it's better to set also DHS_IPCACHE_FILE.
#
#  Caching is automatically turned on in daemonize mode so that dhs.org won't 
#  get DoS.
#
#  DHS_DAEMONIZE=30
# 
#
#
#	 History
#  __________________________________________________________________________
#
#  0.2.0v - Multi OS detection and automatic useage of fetch, and lynx
#  0.1.8v - Daemon mode, caching IP
#  0.1.3v - Using awk
#  0.1.2v - After recovering from my little illness, apart from taking the
#	time to fix some irritating spelling errors, I've added the option
#	to provide all the settings from the command line, so editing 
#	the script by its user is not necessary now.
#  0.1v   - First snappy release. 
#
#
#
#	 Conclusion
#  __________________________________________________________________________
#
#  You can stop reading this file here unless you are interested to see how
#  the script operates.
#  __________________________________________________________________________
#

echo "updatedhs 0.2.0v, beholder@unios.dhs.org"

#
#  getting command-line parameters

CLSWITCH=
for PARAM 
do
	case "$CLSWITCH" in
	  -de)
		case "$PARAM" in
		  0*|1*|2*|3*|4*|5*|6*|7*|8*|9*)
			DHS_DAEMONIZE=$PARAM
			;;
		  *)
			echo seconds were not specified
			;;
		esac
		;;
	  -u)
		case "$PARAM" in
		  -*)
			echo Username unspecified
			;;
		  *)
			DHS_USER=$PARAM
			;; 
		esac
		;;
	  -h)
		case "$PARAM" in
		  -*)
			echo Host unspecified
			;;
		  *)
			DHS_HOST=$PARAM
			;;
		esac
		;;
	  -p)
		case "$PARAM" in
		  -*)
			echo Password unspecified
			;;
		  *)
			DHS_PASSWD=$PARAM
			;; 
		esac
		;;
	  -d)
		case "$PARAM" in
		  -*)
			echo Domain unspecified
			;;
		  *)
			DHS_DOMAIN=$PARAM
			;; 
		esac
		;;
	  -ip)
		IP_METHOD=manual
		IP_MANUAL=$PARAM
		;;
	  -if)
		IP_DEVICE=$PARAM
		;;
	  -os)
		IP_OS=$PARM
		;;
	  -mx)
		DHS_MX=$PARM
		;;
	  *)
		;;
	esac
	CLSWITCH=

	case "$PARAM" in
	  -\? | --help)
		echo "Note: "
		echo "  To update the dynamic host, updatedhs automatically tries to guess"
		echo "  the IP to be updated by running 'ifconfig ppp0'. You can override"
		echo "  this behavior by using the optional -ip switch."

		echo "Usage: "
		echo "  updatedhs [-u username] [-h host] [-p password] [-d domain] [-ip IP]"
		echo
		echo "     -u, --username uname    Sets the username to as specified by 'uname' "
		echo "     -h, --host host         Sets the hostname to as specified by 'host' "
		echo "     -p, --password pass     Sets the password to as specified by 'pass' "
		echo "     -d, --domain domain     Sets the domain to as specified by 'domain' "
		echo "     -ip IP                  Set the dynamic host the the IP specified by IP "
		echo "     -mx MX IP               Set the IP of the MX"

		echo "     -if Interface           Set the interface device to use"
		echo "     -os OS                  Set the OS (Linux, FreeBSD, OpenBSD, or NetBSD)"

	
		echo
		echo "     -de [secs]              Explictly daemonize (check every 'secs' seconds)"
		echo "     -nde                    Explictly don't daemonize"
		echo
		exit 1;
		;;
	  -nde)
			DHS_DAEMONIZE=
			;;
	  -de)
			CLSWITCH=-de
			;;
	  -u | --username)
		CLSWITCH=-u
			;;
	  -h | --host)
		CLSWITCH=-h
			;;
	  -p | --password)
		CLSWITCH=-p
			;;
	  -d | --domain)
		CLSWITCH=-d
			;;
	  -ip)
		CLSWITCH=-ip
			;;
	  -if)
		CLSWITCH=-if
		 	;;
	  -os)
		CLSWITCH=-os
			;;
	  -mx)
		CLSWITCH=-mx
			;;
	  -*)
		echo Invalid switch $PARAM
		exit 0
	esac
done 

if [ "$DHS_USER" = "" ] ; then
	echo "updatedhs: specify the dhs username (-u username) or edit updatedhs"
	exit 0
elif [ "$DHS_PASSWD" = "" ] ; then
	echo "updatedhs: specify the password (-p password) or edit updatedhs"
	exit 0
elif [ "$DHS_HOST" = "" ] ; then
	echo "updatedhs: specify the dhs host (-h host) or edit updatedhs"
	exit 0
elif [ "$DHS_DOMAIN" = "" ] ; then
	echo "updatedhs: specify the dhs domain (-d domain) or edit updatedhs"
	exit 0
fi

DHS_IPCACHE_FILE=$DHS_IPCACHE_DIR/$DHS_HOST.$DHS_DOMAIN.ip.cache

# get operating system with uname

if [ $IP_OS = "default" ] ; then
	IP_OS=$(uname)
fi

# determing what interface to use

if [ $IP_DEVICE = "default" ] ; then

	if [ $IP_OS = "Linux" ] || [ $IP_OS = "NetBSD" ] ; then
		IP_DEVICE=ppp0
	fi
	
	if [ $IP_OS = "FreeBSD" ] || [ $IP_OS = "OpenBSD" ] ; then
		IP_DEVICE=tun0
	fi
	
fi

# determine what updating program to use

if [ $IP_SENDER = "default" ] ; then
	if [ $IP_OS = "Linux" ] ; then
		IP_SENDER=wget
	fi
	
	if [ $IP_OS = "FreeBSD" ] ; then
		IP_SENDER=fetch
	fi

	if [ $IP_OS = "NetBSD" ] || [ $IP_OS = "OpenBSD" ] ; then
		IP_SENDER=lynx
	fi
fi

get_ip() {
#  getting the IP

	GETIP=0
	
	if [ "$IP_METHOD" = "" ]  ||  [ "$IP_METHOD" = "ifconfig" ] ; then
		if [ "$IP_OS" = "FreeBSD" ] || [ "$IP_OS" = "OpenBSD" ] ; then
			AIP=$(ifconfig $IP_DEVICE | grep 'inet ' | \
			awk -F '[ :]+' '{print $2}' | tail -1)
		fi

		if [ "$IP_OS" = "NetBSD" ] ; then
			AIP=$(ifconfig $IP_DEVICE | grep 'inet ' | \
			awk -F '[ :]+' '{print $2}'i | tail -1)
		fi

		if [ "$IP_OS" = "Linux" ] ; then
			AIP=$(ifconfig $IP_DEVICE | grep 'inet ' | \
			awk -F '[ :]+' '{print $4}' | tail -1)
		fi
	
		for BIP in $AIP ; do
			if [ "$GETIP" = "0" ] ; then
				GETIP=$BIP
			fi
		done
	elif [ "$IP_METHOD" = "manual" ] ; then
		GETIP=$IP_MANUAL
	fi

	echo $GETIP
}

LOOPCOUNT=0
LOOPMODE=0

if [ "$IP_MANUAL" != "" ] ; then
	DHS_DAEMONIZE=
fi

if [ "$DHS_DAEMONIZE" != "" ] ; then
	echo "updatedhs: running in daemonized mode, checking IP every $DHS_DAEMONIZE seconds"
	if [ "$DHS_DAEMONIZE" != "" ] && [ "$DHS_IPCACHE" = "" ] ; then
		echo "updatedhs: turning on IP caching"
		DHS_IPCACHE=yes
	fi
fi


if [ "$DHS_IPCACHE_FILE" != "" ] && [ -f $DHS_IPCACHE_FILE ] ; then
	DHS_CACHED_IP="$(cat $DHS_IPCACHE_FILE)"
fi

DHS_URL="http://members.dhs.org/nic/hosts?hostscmd=edit&hostscmdstage=2&type=4&domain=$DHS_DOMAIN&hostname=$DHS_HOST&mx=$DHS_MX&updatetype=Online&ip=$IP"

while [ $LOOPMODE = "0" ] ; do
	if [ "$DHS_DAEMONIZE" = "" ] || [ "$[$DHS_DAEMONIZE > 0]" = "0" ] ; then
		LOOPMODE=1
	fi

   	IP=`get_ip`

	if [ "$IP" = "0" ]  ||  [ "$IP" = "" ] ; then
		echo "updatedhs:  undetermined IP, not updating."
	else
		LOOPCOUNT=$[$LOOPCOUNT+1]
		DO_UPDATE=0

		if [ "$DHS_IPCACHE" = "yes" ] ; then
			if [ "$DHS_CACHED_IP" != "$IP" ] ; then
				DHS_CACHED_IP=$IP
				if [ "$DHS_IPCACHE_FILE" != "" ] ; then
					echo -n "$IP" > $DHS_IPCACHE_FILE
				fi
				DO_UPDATE=1
				echo "updatedhs: $DHS_HOST.$DHS_DOMAIN is $IP (caching)"
				echo "updating ip using $IP_SENDER on $IP_OS..."
			else
			    if [ "$LOOPCOUNT" = "1" ] ; then
				    if [ "$LOOPMODE" = "0" ] ; then
						echo "updatedhs: Waiting for IP to change"
					else
						echo "updatedhs: IP hasn't changed"
					fi
				fi
			fi
		else
			DO_UPDATE=1
			echo "updatedhs: IP is $IP, updating server..."
		fi

		if [ "$DO_UPDATE" = "1" ] ; then
			case "$IP_SENDER" in
				wget)
					wget --http-user=$DHS_USER \
					--http-passwd=$DHS_PASSWD \
					$DHS_URL -O /dev/null
					;;
				fetch)
		export HTTP_AUTH="basic:*:$DHS_USER:$DHS_PASSWD"
					fetch -o /dev/null \
					$DHS_URL 2> /dev/null
					;;
				lynx)
					lynx -dump \
					-auth=$DHS_USER:$DHS_PASSWD \
					$DHS_URL  > /dev/null
					;;
				*)
					;;
			esac
		fi
	fi
	
	if [ "$DHS_DAEMONIZE" != "" ]  &&  [ "$[$DHS_DAEMONIZE > 0]" = "1" ] ; then
		sleep $DHS_DAEMONIZE
	fi
done

exit 1;

--------------47323E51604F9C7F77BD1D6A--