Skip to content

Instantly share code, notes, and snippets.

@raster
Created October 11, 2011 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raster/1279162 to your computer and use it in GitHub Desktop.
Save raster/1279162 to your computer and use it in GitHub Desktop.
Check if the Internet is available
#!/usr/bin/perl
use strict;
use warnings;
use Net::Ping;
my $host = shift || 'www.google.com';
my $wait = shift || 5;
my @states = map { "Internet is $_ " } qw/down up/;
my $p = Net::Ping->new("tcp", 2);
$p->{port_num} = getservbyname("http", "tcp");
my $is_up = 0;
while (1) {
my $state = !!$p->ping($host);
next if $state == $is_up;
$is_up = $state;
print "$states[$is_up]\n";
#if on OS X, talk, otherwise beep
if ($^O eq 'darwin') {
system "say -v Victoria $states[$is_up]";
} else {
print "\a";
}
} continue {
sleep $wait;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment