Everyday Thaumaturgy
(It's a Miracle If I Get Anything Done Around Here)
a blog by William S. Statler

PingRing: simple alarm to tell you when your net connection comes back up

Wed, 27 May 2009 16:00:00 PDT

We live in a semi-rural location, and our broadband connection is a rather slow fixed-wireless system provided by our local electric co-op. I have to give these guys credit for working very hard to keep the system running, but unfortunately it breaks down rather often, sometimes for a minute, sometimes an hour, sometimes a day. It's been down for a few hours now as I type this.

So I got tired of checking and checking to see if I had a connection, and wrote this very simple Linux script to do it for me:

#!/bin/bash

# pingring
# by WSS
# Pings a site until it responds, then plays a tone.
# Requires package "sox" for tone generation.
# Usage:  pingring site
#   where site = an IP address (111.22.333.44) or site name (abc.example.com)

# time to wait for ping response before giving up (seconds):
WAITTIME=3
# sleep time before trying again (seconds):
SLEEPTIME=17

while ! ping -c 1 -w $WAITTIME $1 ; do
  date
  sleep $SLEEPTIME
done

echo "PING RESPONSE RECEIVED"
date
# play a short synthesized tone
play -q -n -c1 synth sin %-24 sin %-9 sin %-5 sin %-2 fade q 0.2 1 0.5

exit 0

This script uses the ping command to attempt to contact a site. It waits for WAITTIME seconds for a response before giving up. Then it prints the current time, and goes to sleep for SLEEPTIME seconds before trying again. This loop repeats until the site replies within the WAITTIME limit, at which point a short tone is played and the script ends. So you can start this running, and go do something else until you hear the tone.

The play command is part of package sox, which is a multipurpose sound-generation and sound-processing tool. If you don't want to install sox, you can modify the script with your own favorite noisemaker, light-flasher, pyrotechnics-launcher, etc.

To use this script, you'll need to choose a site to ping, and that choice depends on what you're trying to find out. If you want to know whether a specific site is up, obviously that's the address you'll want to ping. In my case I want to know if my connection to the Internet exists. So I did some snooping (tracepath from a terminal window, or the Traceroute tab in Ubuntu's Network Tools), and found a branch point on the network through which all my Internet traffic must pass. I call pingring with the IP address of this branch point; when I get a successful response, I can be pretty sure that the local part of my Internet connection is back in operation.

It's generally better to use pingring with a numeric IP address. It will funciton with a domain name (like google.com), but only if you have access to a DNS server, which might not be the case if your network connection isn't working.

Copyright © 2009 William S. Statler (except for quotes and
contributions from other authors). This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
which grants limited rights of non-commercial distribution and reuse. Please read
http://creativecommons.org/licenses/by-nc-sa/3.0/us/
for details. All other rights reserved.