#!/usr/bin/perl use strict; use warnings; use Net::Ping; my @host_array = ("127.0.0.1", "10.0.0.10"); my $p = Net::Ping->new("tcp"); # $p->bind($my_addr); # Specify source interface of pings foreach my $host (@host_array) { print "$host is "; print "NOT " unless $p->ping($host, 2); print "reachable.\n"; sleep(1); } $p->close(); __END__ $ perl test.pl 127.0.0.1 is reachable. 10.0.0.10 is NOT reachable. #### #!/usr/bin/perl use strict; use warnings; use Net::Ping; my @host_array = ('localhost', '10.0.0.10'); # High precision syntax (requires Time::HiRes) my $p = Net::Ping->new(); $p->hires(); foreach my $host (@host_array) { my ($ret, $duration, $ip) = $p->ping($host, 2); if ($ret) { printf("$host [ip: $ip] is alive (packet return time: %.2f ms)\n", 1000 * $duration); } else { printf("$host [ip: $ip] is not alive (packet return time: %.2f ms)\n", 1000 * $duration); } sleep(1); } $p->close(); __END__ $ perl test.pl localhost [ip: 127.0.0.1] is alive (packet return time: 0.11 ms) 10.0.0.10 [ip: 10.0.0.10] is not alive (packet return time: 2002.36 ms)