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

I've downloaded and installed this module but am having trouble using it when passing the hostname to be pinged or I.P. from an array. The script does not write anything to the screen. I don't get any errors and no output....
use strict; use Net::Ping::External qw(ping); my ($server); open (SERVERS, "<serverlist.txt"); my @srvlist = <SERVERS>; foreach $server (@srvlist) { # chop; my $alive = ping(host => "\${server}"); print "$server is online" if $alive; } close(SERVERS);

Replies are listed 'Best First'.
Re: Net::Ping:External
by zigdon (Deacon) on Sep 30, 2002 at 14:37 UTC
    my $alive = ping(host => "\${server}");

    are you really trying to ping a machine called $(server)? try rewriting this line as:

    my $alive = ping(host => $server);

    In a double quoted string, \$ is just escaping the dollar sign, while you want to interpolate the value of $server. And since there is nothing else in the string but $server, might as well skip the interpolation, and just pass the value to ping.

    -- Dan

Re: Net::Ping:External (bug?)
by charnos (Friar) on Sep 30, 2002 at 15:18 UTC
    What operating system are you running this code on? The module says it is still in alpha stage on CPAN, and for Windows systems, it was only tested on 98. I ran the following on Windows 2000, using ActiveState 5.6.1 build 632:
    #!C:\perl\bin\perl use strict; use warnings; use Net::Ping::External qw(ping); print ping(host=>"yahoo.com"), " | ", ping(host=>"wayfakehostname.com" +);
    And received the following output:
    C:\Documents and Settings\marc>perl test.pl 0 | 0
    On my system ping() is returning 0 for both valid and invalid hostnames (unless it didn't correctly perform the ping, I haven't checked the internals yet), which is why nothing is printing out when I run your code, either. Try playing around with the module a bit more, perhaps your output is similar (and/or perhaps you're using ActivePerl and their port is broken).

    Update: There *is* a small bug in the _ping_win32() function of Net::Ping::External (at least the version available through PPM for ActiveState). I changed the last two lines of my own copy from this:
    return 1 if $result =~ /time?\d*ms/; return 0;
    to this:
    return $result =~ /time=\d+ms/?1:0;
    and now it works fine for me. I should probably email the author with that bugfix, perhaps it only is affected in 2k, but I suspect not, the time? will positively match, then because he's missing the '=', the rest fails. I can't imagine that 98's ping prints 'time49ms'.

    oh, and modifying it using fglock's and zigdon's comments, it works like you want it to :)
Re: Net::Ping:External
by fglock (Vicar) on Sep 30, 2002 at 14:49 UTC

    BTW, it's better to use  chomp instead of  chop.

     chomp only eats newlines.

Re: Net::Ping:External
by hopes (Friar) on Oct 01, 2002 at 09:15 UTC
    Oh, well...
    I've tested Net::Ping::External module and the patch by charnos
    Make sure your Win machine has the SO in English
    if don't, /time/ match fails and you obtain that any host is dead...

    Hopes
    perl -le '$_=$,=q,\,@4O,,s,^$,$\,,s,s,^,b9,s,$_^=q,$\^-]!,,print'