#!/bin/bash
#
# $Id: testarp,v 1.4 2002/01/16 09:07:07 andrew Exp $
#
# by Andrew McMillan, Catalyst IT Ltd, (c) 2001 licensed
# for use under the GPL version 2
#
# Thanks to Alexander Clouter for some of the basis of this script :-)
#
#   [$INTERFACE,]$MAC_ADDRESS,$IP_ADDRESS
#
# Turn on execution tracing, for debugging...
[ "$DEBUGWHEREAMI" = "1" ] && set -o xtrace

IFACE=${1/,*}
IP_ADDRESS=${1/*,}
MAC_ADDRESS=${1/,$IP_ADDRESS}
if [ "$IFACE" = "$MAC_ADDRESS" ] ; then
  # We can also set $INTERFACE externally and that will be used as the default.
  INTERFACE=${INTERFACE:-"eth0"}
else
  INTERFACE=${IFACE}
  MAC_ADDRESS=${MAC_ADDRESS/$INTERFACE,}
fi

ifconfig $INTERFACE 0.0.0.0 > /dev/null 2>&1

if (arping -f -w1 -D -I $INTERFACE $IP_ADDRESS | grep -e $MAC_ADDRESS > /dev/null 2>&1) ; then
  # echo "found $MAC_ADDRESS"
  #   Leave the interface running in this case
  RESULT=0
else
  # echo "Not found"
  RESULT=1
fi

exit $RESULT
