#!/bin/sh
#
# 2001-01-30 Tom Kranz (tom@siliconbunny.com)
#
# This script is distributed under the GNU Public License (GPL) with the
# following extra conditions:
# - attritbution must be maintained
# - CD-ROM or similar media for commercial distribution without the prior 
#   approval of the author

# <sigh> I'm being neat and tidy. We return the following error codes:
# 0 - network connectivity is OK as it as - apart from annoyingly printing
#     Woohoo nothing has happened
# 1 - something's fubar on the network, and we've had to swap interfaces
# 2 - you didn't enter a IP address to ping to test connectivity - we'll 
#     print a nice error message and then leave
#

if [ $# -ne 1 ]
then
	echo
	echo "Usage: if_checker.sh <IP_address_to_ping>"
	echo 
	echo "Where <IP_address_to_ping> is an always-reachable address you can"
	echo "use to verify connectivity"
	echo
	exit 2
fi

if /usr/sbin/ping $1 
then
	echo "Woohoo!"
	exit 0
else
	upif="`/usr/sbin/ifconfig -a | grep -v LOOPBACK | grep UP | cut -b 1-4`"
	downif="`/usr/sbin/ifconfig -a | grep -v LOOPBACK | grep -v UP | grep RUNNING | cut -b 1-4`"
	/usr/sbin/ifconfig $upif down
	/usr/sbin/ifconfig $downif up

fi

/usr/sbin/ping $1
exit 1

