View our reviews on Hot Scripts You can now obtain our example scripts and/or vote for them at Hotscripts. To visit Hotscripts click here.

Recently my Internet connectivity has been shotty at best. I use Comcast for an ISP and normally I would assume their service is fine. But Murphy's Law rears it head once again...


For the past few months I have noticed that my internet access/connectivity has been going down for a few hours each day. The times vary, sometimes I am in the middle of something and it goes out, sometimes I am sleeping or out and don't know when it went out. When I wake up and my internet is out I have no way of knowing when it went out so I can't calculate the actual outage time.

Remember folks, I PAY FOR AN ENTIRE MONTHS ACCESS, I should receive the service I pay for!

At anyrate, I needed a way of calculating how much time their service was out so I could begin calculating how much I should deduct from my monthly payment. My solution was a quick script I wrote that:

1) Pings both Google and Yahoo
2) If one of those can be found it writes a 1 to a results file
3) If neither can be reached then we assume no internet connectivity and writes a 0 to the results file.
4) It runs this check once a minute
5) The results file is date stamped for reference

Example: You open the results file and count 120 0's. This means that for 120 minutes there was no connectivity.

Notice in the below screenshot of the results file, I can tell exactly what minute I lost connectivity. In this case it was the 767th minute of the day.


I) This code could easily check more remote sources before deciding on outage.
II) One could easily add a function that automatically counts the 0's in the desired file and display/log the output in easy to read format.
III) This code was written in Perl on Linux. Windows users may need to comment/un-comment some of the code...read the code.



I may write a real windows version, maybe even a GUI program if there is a demand for such...


#!/usr/bin/perl

use strict;
use POSIX;

# Verify results directory, if it is not found then create one
check_dir();

# Run in a continuous loop...
while(1){

# Attempt connection to both Google and Yahoo, write the results
# to 'check' files. If you run linux this section can be edited
# out to avoid writing data to disk. Windows users still may
# need to edit the below system call ? ping and grep args
#system('ping yahoo.com -c1 > yahoo.results; grep "received" yahoo.results > check.yahoo');
#system('ping google.com -c1 > google.results; grep "received" google.results > check.google');
# Grab the size of the 'check' files. 0 size = no connection
#my $google = -s "check.google"; my $yahoo = -s "check.yahoo";

# The above system call and file operations can be edited out and
# the below code can be used if you are running most linux pc's
# Windows does not support `backticks` :( try system()
my $google = `ping google.com -c1 | grep "received"`;
my $yahoo = `ping yahoo.com -c1 | grep "received"`;

# Write the results to a date-stamped text file
my $datestamp = strftime("%Y-%m-%d",localtime) ;
my $results = "results/".$datestamp.".txt\n";

# If connection was possible with either Google or Yahoo then Internet is active
if ($yahoo != 0 || $google != 0){
open (FILE, ">>$results");
print FILE "1\n";
close (FILE);
}
# If Both 'check' files (or $variables) contained zero data
# neither site was available i.e. No Internet
else {
open (FILE, ">>$results");
print FILE "0\n";
close (FILE);
}
# Repeat in 60 seconds. Run checks once a minute
sleep(60);
}

sub check_dir(){
my $dir = 'results/';
unless(-d $dir){
mkdir $dir or die;
}
}

Bookmark / Share:
StumpleUpon Ma.gnolia DiggIt! Del.icio.us Blinklist Yahoo Furl Technorati Simpy Spurl Reddit Google


Do you have a thought about this article? Post a comment and tell us about it!