Created
October 11, 2011 19:35
-
-
Save raster/1279162 to your computer and use it in GitHub Desktop.
Check if the Internet is available
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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