Checking internet connectivity

posted on 07:53 AM on Tuesday 21 October 2014

Pauline was complaining of internet connectivity issues at home which was rather random and prolonged. Each time, connectivity would drop for an hour or 2 and then it would suddenly work without intervention. This is really strange and has happened in the past but it was not common. It appears to be more frequent recently or it could be that Pauline is at home more often and notices it. So I wrote a small naive Python script to ping Google and placed that in the crontab to run every five minutes. The following is the script if it helps anyone.

#!/usr/bin/env python
import urllib2
import datetime
try:
  r = urllib2.urlopen("http://www.google.com")
  outfile = open("/home/bernett/google_ping.txt", "a")
  outfile.write("%s - HTTP code %s\n" % (datetime.datetime.now().strftime("%Y%m%d %H:%M:%S"), r.getcode()))
  outfile.close()
except:
  outfile = open("/home/bernett/google_ping.txt", "a")
  outfile.write("%s - %s\n" % (datetime.datetime.now().strftime("%Y%m%d %H:%M:%S"), "ERROR - failed to connect to Google"))
  outfile.close()

bernett.net