tags:

views:

46

answers:

2

Hi 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?

+6  A: 

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.

daxim
hmm you are correct. But I just wanted to do basic checking before i could process something. thats why. Thanks bro..
shayam
in my program i use expect to do ssh. in that i 've given [email protected];$exp->spawn("ssh $argument") or die "Cannot spawn ssh to ip $ssh_ip_address\n";But even if it is not able to do ssh it is not printing Cannot spawn ssh to ip $ssh_ip_address. can you please help me why it does not occur?
shayam
[Open a new question](http://stackoverflow.com/questions/ask) for this different problem.
daxim
+1  A: 

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
}
d5ve