counter - Counting lost packets with perl -
i'm writing small script returns pingtimes given host. far working should want able see how many packets lost.
when run standard ping command in windows command prompt this:
ping-statistic 173.194.70.138: packets: sent = 4, received = 4, lost = 0 (0%)
how can make perl count everytime packet lost? there way invoke windows commands within perl?
my current code below:
#!/usr/bin/perl use warnings; use strict; use time::hires; use net::ping; use vars qw($argv $ret $duration $ip); $host = $argv[0] or print "usage is: $0 host [timeout]\n" , exit 1; $timeout = $argv[1] || 5; $p = net::ping->new('icmp', $timeout); if ($p->ping($host)) { $p->hires();{ ($ret, $duration, $ip) = $p->ping($host); printf("$host [ip: $ip] online (packet return time: %.2f ms)\n", 1000*$duration); } $p->close(); }else{ print "no such host, timeout of $timeout seconds reached\n"; }
thanks in advance!
if hostname cannot found or there problem ip number, success flag returned undef. otherwise, success flag 1 if host reachable , 0 if not.
so $p->ping
can return undef
, 1
, or 0
my $lost = 0; $n = 10; while ($n--) { # die if ping returns undef $ok = $p->ping($host) // die "no such host, timeout of $timeout seconds reached\n"; $lost++ if !$ok; } print "$lost lost packets\n";
Comments
Post a Comment