Netzwerkanalyse, Fehlersuche, Testberichte
Home / News
Netzwerkanalyse
Grundlagen
Verkabelung
Erste Schritte
Baselining
Let's sniff
Tools
Auswerten/Filtern
Typische Probleme 1
Typische Probleme 2
Sicherheit
Bücher
Tutorials
Cacti
DSL einrichten
DynDNS einrichten
m0n0wall | pfSense
Nmap
VirtualBox
Wireshark
FAQ
Know How
Testberichte
Hardware-DB
Netzwerklexikon
Links / Service
Suche
Kontakt
Impressum
Feedback
Sitemap
Tutorials

ether-watch - Tool zum Triggern von DHCP

ether-watch beobachtet via mii-diag die Netzwerkkarte und beim Stecken des Kabels versucht per DHCP eine IP-Adresse zu erhalten.


#!/bin/sh
#
# ether-watch
#
# watch the state of the ethernet-interface with mii-diag 
# and triggers dhcpcd (or whatever you like)
#
# Mirko Kulpa, 2003
#
# you need Donald Becker's "mii-diag" from http://www.scyld.com/diag/
#
#
#  	This program is free software; you can redistribute it and/or modify
#  	it under the terms of the GNU General Public License as published by
#  	the Free Software Foundation; either version 2 of the License, or
#  	(at your option) any later version.
#
#  	This program is distributed in the hope that it will be useful,
#  	but WITHOUT ANY WARRANTY; without even the implied warranty of
#  	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  	GNU General Public License for more details.
#
#  	You should have received a copy of the GNU General Public License
#  	along with this program; if not, write to the Free Software
#  	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

trap 'MSG="terminating for $INT"; print_msg; dhcpcd -k $INT; exit' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
 
#
# print msg to stdout and syslog
#
print_msg()
{

	echo "ether-watch[$$]: $MSG"
	logger "ether-watch[$$]: $MSG"

}



### main ###

if [ $# = 0 ]
	then
		INT="eth0"
	else
		INT=$1
fi

MSG="running for $INT"
print_msg

LINK=0

while true  
do

mii-diag --status $INT > /tmp/ether-watch-$INT-status.tmp

if [ $? = 0 ]
then
	case "$LINK" in
		0)
			# start dhcpcd, up interface
			MSG="new link beat detected on $INT"
			print_msg
			#echo -n "ether-watch[$$]:"
			#grep -E "partner|media" /tmp/ether-watch-$INT-status.tmp
			
			
			if [ -f /etc/dhcpc/dhcpcd-$INT.pid ]
				then
					MSG="dhcpcd already running for $INT , trying to kill"
					print_msg
					dhcpcd -k $INT
					sleep 2
			fi
			MSG="starting dhcpcd for $INT"
			print_msg
			dhcpcd -t 20 -d $INT
			;;
		1|2)
			# do nothing, dhcpcd always running
			# echo "ether-watch[$$]: link beat detected"
			;;
	esac

	LINK=2
else
	case "$LINK" in
		0) 
			# do nothing
			# echo "ether-watch[$$]: no link beat"
			;;
		1)
			# stop dhcp, down interface
			MSG="link beat lost on $INT, terminating dhcdcd"
			print_msg
			dhcpcd -k $INT
			LINK=0
			;;
		2)
			# wait another period until down interface
			MSG="maybe link beat lost on $INT"
			print_msg
			LINK=1
			;;
	esac
fi

sleep 2

done

 

 
© 2004-2023, network lab - we make your net work - Netzwerkforum
aktualisiert am 22.03.2012