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

Hi , I want to ping with perl a machine, when i used this code :
use Net::Ping; $host=127.0.0.1; $p = Net::Ping->new(); print "$host is alive.\n" if $p->ping($host); $p->close();
and run perl ping.pl, i didnot get any answer,and it doesn't print any things.is it correct or not. thanks for ur help.

Edit: Added <code> tags, larsen.

Replies are listed 'Best First'.
Re: ping problem
by jasonk (Parson) on Feb 28, 2003 at 15:17 UTC

    What you are pinging isn't a valid IP address, you need to put quotes around it. $host = "127.0.0.1".

      The following syntax is also valid:

      $host = v127.0.0.1
      This will do the Right Thing, but only from Perl 5.6 on.

      --
      mowgli

        so thanks for ur help but i have other problem: Hi dear I am using Redhat 7.3 and SuSE8.0 and i want to ping on special port a server to know is it alive or not. at the first, when i run the example on cpan.org(Net::ping) it doesnt answer any, i download this module and installed it, then ping(icmp) replay. but when i want to ping on specified port with tcp or syn portocol it replay ok(no differnce that service is up or down). i mean this code: $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(3); #} undef($p); it respond ok no difrence that http service is up or down . i use ur fping : ./fping 192.168.100.12 -P 80 it shows alive in each time that service is up or down. how could i solve the problem. and what is my fault. why it shows host is alive in each time. it is important for me. Best regurds R.A