I Automated What You Do When Your Internet Connection Drops

The East Coast suffered Hurricane Sandy today. With the lights flickering off for brief moments every half hour or so, I decided it was time to stop hastily typing “ping” commands to diagnose my connectivity problems and so I wrote something to do it better. The result is a script that intelligently tests your networking hardware for connectivity.

It does 3 things:

  1. Ping the local gateway to make sure the hardware is working
  2. Ping the ISP to make sure that link is working
  3. Ping an Internet address to make sure that connectivity exists

Blasting ICMP packets at the Internet as fast as you can is not considerate. You want to ping your gateway quickly so that you know immediately when it comes up. Once you have internet connectivity though, you can ping more slowly.

The script pauses between successful Internet pings. Failing that, it polls more quickly (depending on how much connectivity you actually have).

Output from the command is similar to the ping utility, with a timestamp and network status on each line:

$ pingout.sh
PingOut: 12:48:41 Internet
PingOut: 12:48:51 Internet
PingOut: 12:49:02 Internet
PingOut: 12:49:12 (No Internet) (ISP FAIL) Gateway
PingOut: 12:49:16 (No Internet) ISP
PingOut: 12:49:21 Internet
PingOut: 12:49:32 Internet
PingOut: 12:49:42 Internet ^C
$

In this example, my ISP had a hiccup at 12:49:12, and regained a connection sometime afer 12:49:16. Failure messages can either be “No ISP/Gateway” or “ISP/Gateway FAIL” — the former indicating that there is no route available and the latter meaning that a route exited, but pinging that router failed.

Here’s another example of a brief power outage that reset my modem and router:

$ pingout.sh
PingOut: 11:44:55 Internet
PingOut: 11:45:05 Internet
PingOut: 11:45:15 Internet
PingOut: 11:45:25 (No Internet) (No ISP) (No Gateway)
PingOut: 11:45:30 (No Internet) (No ISP) (No Gateway)
PingOut: 11:45:34 (No Internet) (No ISP) (No Gateway)
PingOut: 11:45:38 (No Internet) (No ISP) (No Gateway)
PingOut: 11:45:42 (No Internet) (No ISP) (No Gateway)
PingOut: 11:45:46 (No Internet) (No ISP) (No Gateway)
PingOut: 11:45:50 (No Internet) (No ISP) (No Gateway)
PingOut: 11:45:54 (No Internet) (No ISP) (No Gateway)
PingOut: 11:45:58 (No Internet) (No ISP) (No Gateway)
PingOut: 11:46:02 (No Internet) (No ISP) (No Gateway)
PingOut: 11:46:06 (No Internet) (ISP FAIL) Gateway
PingOut: 11:46:10 (No Internet) (ISP FAIL) Gateway
PingOut: 11:46:14 (No Internet) (ISP FAIL) Gateway
PingOut: 11:46:18 (No Internet) (ISP FAIL) Gateway
PingOut: 11:46:22 (No Internet) (ISP FAIL) Gateway
PingOut: 11:46:26 (No Internet) (ISP FAIL) Gateway
PingOut: 11:46:30 (No Internet) (ISP FAIL) Gateway
PingOut: 11:46:34 (No Internet) ISP
PingOut: 11:46:39 Internet
PingOut: 11:46:49 (No Internet) ISP
PingOut: 11:46:54 Internet
PingOut: 11:47:04 Internet^C
$

Note that you do not need to type in any IP addresses; the gateway address and ISP address are determined automatically, and the internet address is hard coded (4.2.2.2, a favorite for connectivity tests).

You can download the contents of the pingout.sh script or copy it from the listing below:. The code listing was removed since it is consistently out of date. The download is linked to a git repository with the latest version.