how to check whether the remote machine is pingable in perl
I need to do check whet开发者_StackOverflow社区her the remote machine is pinging so that i can do ssh to that machine and execute commands over there. how to do this check in perl?
Net::Ping
, but this is a dumb way to go about it. If you want to connect with SSH, just do it, and handle the failure if you cannot.
If a network interface responds to ping, this is no guarantee that SSH works. Conversely, SSH can work and ping is blocked.
A simple and fast test that a host is listening on a particular port can be done using IO::Socket::INET
use IO::Socket::INET;
my $address_tuple = "$ip:$port";
my $test_sock = IO::Socket::INET->new(PeerAddr => $address_tuple, Timeout => 0.15);
if ( $test_sock) {
# Host is up and appears to be listening on that port
$test_sock->close();
}
else {
# Host doesn't appear to be listening on that port
}
精彩评论