Auto-repair flaky network connections with netrepaird

Use Linux? Have a flaky cable modem or DSL hookup? If you do, and you leave your computer unattended, you’ll return to find zillions of network error dialogs. Here’s a quick solution.

The netrepaird script

First off, here’s the netrepaird script. It’s a simple script that attempts to ping network addresses (that you can define yourself), and if all pings fail, it’ll attempt progressively agressive measures to repair your network connection. It’s really low-tech, but it works.

There’s an updated, more featureful script here.

#!/bin/bash

# edit these variables to suit your needs HOSTS="rudd-o.com gplhost.com mozilla.org" # define hosts to ping here INTERFACE="eth1" # define the name of your network interface DRIVER="cdc_ether" # define the driver (kernel module) of your network interface

SUDO="sudo" # define to use sudo

netconnected() {

for host in $HOSTS; do NETCONNECTED=no $SUDO ping -c 4 $host > /dev/null [ "$?" == "0" ] && { NETCONNECTED=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

}

netconnected [ "$NETCONNECTED" == "yes" ] && { exit ; } # network is OK

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 "Network is definitely down" logger -t netrepaird -p local0.warn "Network is down, could not repair connection" exit 1

Installing this script

Put this in the file named /usr/local/bin/netrepaird and make it executable (chmod +x /usr/local/bin/netrepaird).

Making it run every minute

That’s right, it’d make no sense to just chuck the script on your hard disk. For this, we’re going to make it run periodically, every minute. Edit your administrator’s crontab file:

  • Ubuntu users: sudo crontab -e
  • Fedora users: su -c 'crontab -e'

and place this line on the crontab file:

* * * * * /usr/local/bin/netrepaird

Small gotcha: make sure you put a carriage return at the end of the file, otherwise cron doesn’t execute the command.

That’s it

That’s it. Now, every minute, the script will run and check for an available Internet connection. If the connection flakes out for whatever reason, it’ll attempt to take it down and up, and if that fails, it’ll attempt to remove and reload the device driver and restart the connection.

And if those measures fail, your root user will get an e-mail with the warning, and a message will be duly noted in the system log file.

Packagers?

If any of my readers is interested in packaging this for a distribution (which would entail separating the configuration from the script), please contact me to get help.

Happy hacking!

Leave a Reply