[svlug] how to detect ethernet cable is plugged in

HvR hvrietsc at myrealbox.com
Thu May 1 14:24:59 PDT 2003


Here is the script i build on top of the mii-tool on redhat, i run it
from rc.local. feel free to use or abuse at your own risk. Thanks again
to all that answered my query.

#!/bin/sh

#-----------------------------------------------------------------
# debug
#-----------------------------------------------------------------
debug() {
:
#	echo "$0: $@"
}

#-----------------------------------------------------------------
# log status
#-----------------------------------------------------------------
log() {
	logger -t "$0" "eth0 $NEWSTATUS"
}


#-----------------------------------------------------------------
# echo PLUGGED or UNPLUGGED
#-----------------------------------------------------------------
status() {
	case `/sbin/mii-tool eth0 ` in
	*link\ ok*)	echo "PLUGGED" ;;
	*)		echo "UNPLUGGED" ;;
	esac
}


#-----------------------------------------------------------------
# bring eth0 up or down
#-----------------------------------------------------------------
ifupordown() {
	case $1 in
	PLUGGED)	debug "ifupping"
			ifup eth0 >/dev/null 2>&1
			;;
	UNPLUGGED)	debug "ifdowning"
			ifdown eth0 >/dev/null 2>&1
			;;
	*)		echo "ERROR in $0/ifupordown"
			exit 1
			;;
	esac
}

PATH=/sbin:$PATH
OLDSTATUS=`status` ; debug "OLDSTATUS=$OLDSTATUS"
ifupordown $OLDSTATUS

while true
do
	NEWSTATUS=`status` ; debug "NEWSTATUS=$NEWSTATUS"
	if [ $NEWSTATUS != $OLDSTATUS ]
	then
		debug "CHANGE" ; log
		ifupordown $NEWSTATUS
	else
		debug "no change"
	fi
	OLDSTATUS=$NEWSTATUS
	sleep 5s
done



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.svlug.org/archives/svlug/attachments/20030501/0a9ab259/attachment.htm


More information about the svlug mailing list