/dev/null...">

How to get an answer from "ping -c 1 example.com"?

In BASH can I ping the server as such

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 to get an answer from

 my $response = `ping -c 1 google.com > /dev/null 2>&1` 

Question

How can I do the same in Perl, but without using any packages like Net::Ping ?

+4
source share
2 answers

You are interested in the ping exit code, not the output; forget about $response and check the exit code in $? .

+4
source

I would 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

+1
source

Source: https://habr.com/ru/post/1379622/


All Articles