Reputation: 25986
In BASH can I ping a server like so
for i in $MY_SERVER_LIST; do
if ping -c 1 $i > /dev/null 2>&1; then
# $i is alive
fi
done
and I would like to do the same in Perl, but how do I get the response from
my $response = `ping -c 1 google.com > /dev/null 2>&1`
Question
How do I do the same in Perl, but without using any packages like Net::Ping
?
Upvotes: 4
Views: 3286
Reputation: 43
I'd use Net::Ping !
use Net::Ping;
$p = Net::Ping->new();
print "$host is alive.\n" if $p->ping($host);
$p->close();
$p = Net::Ping->new("icmp");
$p->bind($my_addr); # Specify source interface of pings
foreach $host (@host_array)
{
print "$host is ";
print "NOT " unless $p->ping($host, 2);
print "reachable.\n";
sleep(1);
}
$p->close();
http://perldoc.perl.org/Net/Ping.html
Upvotes: 1
Reputation: 122381
You are interested in the exitcode of ping
not the output; forget about the $response
and examine the exitcode in $?
.
Upvotes: 4