netrepaird
#!/bin/bash
NETHOSTS="rudd-o.com gplhost.com mozilla.org" # network hosts to check
ROUTERIPS="200.63.232.1" # IPs in the immediate physical neighbor network
INTERFACE="eth1" # network interface name
DRIVER="cdc_ether" # device driver operating the network interface
#SUDO="sudo" # uncomment to use sudo, if you're running this as non-root
netconnected() {
for host in $NETHOSTS; do
NETCONNECTED=no
$SUDO ping -c 4 $host > /dev/null
[ "$?" == "0" ] && { NETCONNECTED=yes ; return ; }
done
}
routersaccessible() {
for host in $ROUTERIPS; do
ROUTERSACCESSIBLE=no
$SUDO ping -c 4 $host > /dev/null
[ "$?" == "0" ] && { ROUTERSACCESSIBLE=yes ; return ; }
done
}
ifdownifup() {
$SUDO /sbin/ifdown "$INTERFACE"
$SUDO /sbin/ifup "$INTERFACE"
}
reloaddriver() {
$SUDO /sbin/ifdown "$INTERFACE"
$SUDO /sbin/modprobe -r "$DRIVER"
$SUDO /sbin/modprobe "$DRIVER"
$SUDO /sbin/ifup "$INTERFACE"
}
# this prevents two netrepaird instances from running simultaneously
lockfile="/var/lock/netrepaird-$INTERFACE"
if ( set -o noclobber; echo "$$" > "$lockfile") 2> /dev/null;
then
trap 'rm -f "$lockfile"; exit $?' INT TERM EXIT
else
echo "Failed to acquire lockfile: $lockfile."
echo "Held by $(cat $lockfile)"
exit 4
fi
netconnected
[ "$NETCONNECTED" == "yes" ] && { exit ; } # network is OK
echo "Network problem, verifying neighbor network connectivity"
routersaccessible
[ "$ROUTERSACCESSIBLE" == "yes" ] && { echo "Your network connection is OK, but your ISP has network problems" ; logger -t netrepaird -p local0.warn "Network connection OK but the ISP is having network problems" ; exit 2 ; }
echo "Network problem, toggling network interface $INTERFACE"
ifdownifup ; netconnected
[ "$NETCONNECTED" == "yes" ] && { echo "Network OK after interface toggle" ; exit ; }
echo "Network problem, reloading driver"
reloaddriver ; netconnected
[ "$NETCONNECTED" == "yes" ] && { echo "Network OK after driver reload" ; exit ; }
echo "Could not repair your network connection"
logger -t netrepaird -p local0.warn "Could not repair network connection"
exit 1
