#!/bin/sh

if [ `id -u` -ne 0 ] || [ "$1" = "" ]; then exit 1; fi

if [ -x /usr/bin/fping ]; then
        PING="/usr/bin/fping"
else
        PING="/bin/ping -c 2"
fi

iface="$1"
which=""

while read addr pingme scheme; do
        if [ "$which" ]; then continue; fi

        ifconfig $iface $addr >/dev/null 2>&1
        ifconfig $iface up

        if $PING $pingme >/dev/null 2>&1; then
                which="$scheme" 
        fi
        ifconfig $iface down
done

if [ "$which" ]; then echo $which; exit 0; fi
exit 1

