If you have a Raspberry Pi on WiFi then you are probably aware of the struggles with it randomly disconnecting. If anything goes wrong with the WiFi (access point restarts, IP address changes, you look at the pi the wrong way, etc.) the WiFi will drop and never fix itself. I've written a script called net-dev-kick to kick the network device if a ping check fails.

https://github.com/bahamas10/net-dev-kick

There are a lot of small scripts floating around forums that do what net-dev-kick does (albeit, poorly and with a lot of assumptions) so I decided to clean it all up and make a simple yet robust program to deal with this situation.

This script attempts to ping a server that should always be up (use your router or something similar) and if it fails, it restarts the interface given as $1.

net-dev-kick is intended to be used on the raspberry pi to fix wlan0, but can theoretically be used on any machine to fix any interface. It's best used as a cronjob that runs every 5 or so minutes.

Usage

Usage: net-dev-kick [-t <tries>] [-w <deadline>] <server|ip> <int1> [int2 ...]

Options:

  -h              print this message and exit
  -t <tries>      number of times to try the ping command, defaults to $tries
  -w <deadline>   passed to ping as -w, timeout in seconds for the ping command, defaults to $deadline

Example:

  $ net-dev-kick -t 5 -w 5 10.0.1.1 wlan0

This will attempt to ping 10.0.1.1 (presumably a router) 5 times
(maximum, -t 5) with a forced timeout on the ping command of 5 seconds
(-w 5).

If any ping is succesful, this program will exit cleanly.  If all 5 pings
fail, the interface wlan0 will be brought down and back up.