#!/bin/sh
#
#   Script to assist in locating us on a particular network by confirming
#   that we manage to hit a specified host with a quick 'ping'. We actually
#   use 'fping' because it's waaay quicker.
#
#   Written by Andrew McMillan <andrew@mcmillan.net.nz>, 10th December 1999
#
#   $1 is the IP we are trying to find
#   $2 is the IP number we should use on that subnet
#

mynet=`echo $1 | cut -d'.' -f1-3`
myip=$mynet.$2

ifconfig eth0 $myip >/dev/null 2>&1

#   Note that the timeout on fping is set very low (30 mS - -t30) so that 
#   the host will need to be close for this to work.  Most LANs will
#   get a response in well under 30mS, but we kind of insist because
#   we're only gonna retry once... (-r1).
#
if (fping -a -b12 -B1.2 -i5 -r1 -t30 $1) ; then
  # echo "found $1"
  #   Leave the interface running in this case
  RESULT=0
else
  # echo "Not found"
  RESULT=1
  #   Downing the interface might seem a good idea, but it makes our next
  #   attempt much slower.  Better to down it after all attempts have failed.
  # ifconfig eth0 down
  ifconfig eth0 0.0.0.0 >/dev/null 2>&1
fi

exit $RESULT
