Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

when no ping say 'moo!'

by Discipulus (Canon)
on Feb 25, 2009 at 12:13 UTC ( [id://746236]=CUFP: print w/replies, xml ) Need Help??

aah ahh not so cool but handy maybe!!
linux peoples can rearrange easely.
run as follow: Aping.pl -i 10.0.0.1
or without args to get help

Lor*
#!perl use strict; use warnings; $|++; use Getopt::Long; use Win32::Sound; my $sleep = 60; my $conteggio = 4; my $echook = ( $conteggio - 1 ); my $alarmnum = 3; my $sound; my $volume = 100; my $host; &aiuto() if ( ! GetOptions ( 'ip=s' => \$host, 'sleep=i' => \$sleep, 'conteggio=i' => \$conteggio, 'ok=i' => \$echook, 'alarm=i' => \$alarmnum, 'play=s' => \$sound, 'volume=i' => \$volume, ) or ! $host ); $alarmnum = 3 if $alarmnum < 1; if ( $echook > $conteggio ){ $echook = $conteggio-1 } if (defined $sound && !(-e $sound)) {$sound = 'crow.wav'} while (1){ sleep $sleep; my $ok = &controlla($host); if ($ok < $echook){ for (1..$alarmnum) { &suona() } print "ERROR !\n"; } else {print "OK $host\n";} } sub suona { if (defined $sound && -e $sound) { Win32::Sound::Volume($volume,$volume); Win32::Sound::Play($sound); } else {sleep 1; print"\a\a"} } sub controlla{ my $ok =0; open CMD,"ping -n $conteggio -l 1 $host 2>&1|"; while (<CMD>) {++$ok if $_ =~/$host/;print"."} close CMD; return $ok; } sub aiuto { print <<EOA; USAGE OF $0: $0 -i 10.0.0.1 [-sleep n] [-conteggio n] [-ok n] [-alarm n] [-play + file.wav] [-volume %] -i 10.0.0.1 ping specified IP every 60 seconds 4 times expecting 3 positives repl +y or play 3 system bell during 3 seconds --ip=10.10.0.1 --sleep = 30 --conteggio = 10 --ok = 5 --alarm = 20 -i 10.10.0.1 -s 30 -c 10 -o 5 -a 20 same of above EOA exit 1; }

Replies are listed 'Best First'.
Re: when no ping say 'moo!'
by ambrus (Abbot) on Feb 25, 2009 at 19:38 UTC

    The ping program actually gives the right exit code, so you can just look at that instead of trying to parse its output.

Re: when no ping say 'moo!'
by jasonk (Parson) on Feb 25, 2009 at 20:58 UTC

    The OS X version doesn't even need perl!

    # ping -c1 hostname || say 'moo!'

    www.jasonkohles.com
    We're not surrounded, we're in a target-rich environment!
Re: when no ping say 'moo!'
by ruzam (Curate) on Feb 25, 2009 at 14:53 UTC
    I can't test (no win32 here), but I would replace the ping command with Net::Ping
    use Net::Ping; ... sub controlla{ my $ok = 0; my $p = Net::Ping->new("icmp"); my $tries = $conteggio; while ($tries-- > 0) { $++ok if $p->ping($host); } return $ok; }
Re: when no ping say 'moo!'
by Discipulus (Canon) on Feb 26, 2009 at 10:00 UTC
    ok u rigth.. why parse the output ?? use pure Perl!
    here the new version
    some idea to play sound in other system? like if $^O ne 'MSWin32' ...
    #!perl use strict; use warnings; $|++; use Getopt::Long; use Net::Ping; use Win32::Sound; my $sleep = 60; my $conteggio = 4; my $echook = ( $conteggio - 1 ); my $alarmnum = 3; my $sound; my $volume = 100; my $timeout = 2; my $host; &aiuto() if ( !GetOptions('ip=s' => \$host, 'sleep=i' => \$sleep, 'conteggio=i'=>\$conteggio, 'ok=i' => \$echook, 'timeout=i' => \$timeout, 'alarm=i' => \$alarmnum, 'play=s' => \$sound, 'volume=i' => \$volume, ) or ! $host ); $timeout = 2 if $timeout < 1; $alarmnum = 3 if $alarmnum < 1; if ( $echook > $conteggio ){ $echook = $conteggio-1 } if (defined $sound && !(-e $sound)) {$sound = 'crow.wav'} ###################################################################### +########## while (1){ sleep $sleep; my $ok = &controlla($host); if ($ok < $echook){ for (1..$alarmnum) { &suona() } print " ERROR !\n"; } else {print " OK $host\n";} } ###################################################################### +########## sub controlla{ my $host = shift; my $p = Net::Ping->new("icmp"); my $ok = 0; foreach my $try (1..$conteggio) { print ($p->ping($host, $timeout) ? (++$ok and '.') : 'X' ); sleep(1); } $p->close(); return $ok; } ###################################################################### +########## sub suona { if (defined $sound && -e $sound) { Win32::Sound::Volume($volume,$volume); Win32::Sound::Play($sound); } else {sleep 1; print"\a\a"} } ###################################################################### +########## sub aiuto { print <<EOA; UTILIZZO DI $0: $0 -i 10.0.0.1 [-sleep n] [-conteggio n] [-ok n] [-timeot n] [-alar +m n] [-play file.wav] [-volume %] -i 10.0.0.1 ping specified IP every 60 seconds 4 times expecting 3 positives repl +y or play 3 system bell during 3 seconds --ip=10.10.0.1 --sleep = 30 --conteggio = 10 --ok = 5 --alarm = 20 -i 10.10.0.1 -s 30 -c 10 -o 5 -a 20 same of above EOA exit 1; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://746236]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2024-04-18 10:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found