http://qs1969.pair.com?node_id=448069

perlknight has asked for the wisdom of the Perl Monks concerning the following question:

All, below is a code snipet from the doc of net ping. AFAIK, this is suppose to test if www port is open (listening).
$p = Net::Ping->new("tcp", 2); # Try connecting to the www port instead of the echo port $p->{port_num} = getservbyname("http", "tcp"); #while ($stop_time > time()) #{ print "$host not reachable ", scalar(localtime()), "\n" unless $p->ping($host); sleep(300); #} undef($p);
I tried using this, but it seems to fail. I changed "www" to ssh and telnet. I ran it on a host that should have failed. Anyone knows what this did not fail?

Replies are listed 'Best First'.
Re: net ping question
by gellyfish (Monsignor) on Apr 15, 2005 at 09:48 UTC

    You probably don't want to use Net::Ping to this as this will use the TCP echo service and all you are doing is changing the port to which it tries to connect to this service. I would suggest if you just want to check whether you have a particular port open on a particular machine you do something like:

    use IO::Socket; + my $sock = IO::Socket::INET->new(PeerAddr => 'localhost', PeerPort => 'http(80)', Proto => 'tcp'); + + print $sock ? "alive" : "not alive";

    /J\

      Thanks. Will try it out and see if it works.
Re: net ping question
by moot (Chaplain) on Apr 15, 2005 at 04:43 UTC
    You mention changing 'www' to ssh and telnet, but the only place www is mentioned in your code is a comment. Also your last few sentences aren't clear. Are you saying that pinging ssh/ telnet should fail on the host you specify? Are you sure that the server has something listening on the http port?