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

using net ping 2.02

i tried to use this feature a million times but im not having any luck. no matter what i try it always fails even when pinging 127.0.0.1


here is from the man page... i tried all of these
use Net::Ping; $p = Net::Ping->new(); print "$host is alive.\n" if $p->ping($host); $p->close(); $p = Net::Ping->new("icmp"); foreach $host (@host_array) { print "$host is "; print "NOT " unless $p->ping($host, 2); print "reachable.\n"; sleep(1); } $p->close(); $p = Net::Ping->new("tcp", 2); while ($stop_time > time()) { print "$host not reachable ", scalar(localtime()), "\n" unless $p->ping($host); sleep(300); } undef($p); # For backward compatibility print "$host is alive.\n" if pingecho($host);

Replies are listed 'Best First'.
Re: Net:Ping HELP!
by TomDLux (Vicar) on May 29, 2003 at 02:02 UTC

    $host does not have a value. Try inserting line 2:

    my $host = "127.0.0.1";

    Similarly, @host_array and $stop_time are undef.

      If Tom's suggestion still doesn't work for you.. you will have to read the fine print which tells you that the module has to be run as root (or administrator if you are on Win32), so add some error checking. If you are on Win32, you can use Win32::PingICMP to do the same thing without administrator rights. If you need to do this via a CGI script... then you will have to set up your web server to run the script with admin/root privs... :-) you can also just use backticks and grab the output yourself and parse it yourself... my $ping = `ping 127.0.0.1`; Don't re-invent the wheel, there are a ton of posts on this... do a search in here... JamesNC
Re: Net:Ping HELP!
by hangmanto (Monk) on May 29, 2003 at 04:08 UTC
    This is an excellent example of the value of inserting
    use strict; use warnings;
    at the top of every script. It may seem tedious, but the time and frustration you save will be well worth it.

      sorry i should have posted the actual script. here it is. It is being run as root on a FreeBSD 4.7 box.


      It takes about 1 second to run this script, but then I get no output at all.

      #!/usr/bin/perl -w use strict; use Net::Ping; my $host = "127.0.0.1"; my $ping=Net::Ping->new(); while ($ping->ping($host,1)) { print "down\n"; }
        You left out what protocol to use... try this snippet
        #!/perl/bin/perl -w use strict; use Net::Ping; my $host = "127.0.0.1"; my $ping=Net::Ping->new("icmp"); if ($ping->ping($host,1)){ print "$host is alive"; }else{ print "$host unreachable" }

        Update: added else statement