Raj
Raj

Reputation: 767

how to check whether the remote machine is pingable in perl

I need to do check whether 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?

Upvotes: 0

Views: 824

Answers (2)

d5ve
d5ve

Reputation: 1160

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
}

Upvotes: 1

daxim
daxim

Reputation: 39158

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.

Upvotes: 6

Related Questions