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

Hello all, I am fairly new to perl, having learned it 10 years ago by self training, but then didn't use it until recently. I have some to ping an IP address using a subroutine that works with an array, or with an IP from STDIN, but not from a straight variable. I'm confused! Any help would be appreciated! Thanks! Here are 3 examples of the code, 2 that work, and the one I really need to work that doesn't:

# this does not work use strict; use Net::Ping; use warnings; my $ip = qw(172.16.1.2); # my $works = &pingIp($ip); # if ( $works == 1 ) { print "does not ping\n"; } else { print "pings\n"; } sub pingIp { my $ip1 = shift; my $p = Net::Ping->new(); if ($p->ping($ip1)) { print "return is 0\n"; return 0; } else { print "return is 1\n"; return 1; } $p->close(); }

If I prompt for an IP rather than set the IP outright, the above works

print "Enter an IP: "; chomp (my $ip=<STDIN>);

Or, if I create an array of IPs and ping each one using the pingIp subroutine, it works

my @IParray = qw(172.16.1.2 172.16.1.3); foreach my $ip (@IParray) { my $works = &pingIp($ip); if ( $works == 1 ) { print "$ip does not ping\n"; } else { print "$ip pings\n"; } }

Thanks for any help!

Replies are listed 'Best First'.
Re: Ping not working
by VinsWorldcom (Prior) on Mar 11, 2015 at 14:52 UTC

    Note that by default Net::Ping uses TCP as the default protocol (TCP echo port 7) which some systems may not respond to even if they are up. To use ICMP, you'll need to set the following AND be root / have admin privilege:

    my $p = Net::Ping->new('icmp');
Re: Ping not working
by toolic (Bishop) on Mar 11, 2015 at 13:32 UTC
    FWIW, I tried your 3 methods, and all 3 give me the same result on the 172.16.1.2 ip: "does not ping".

    Other observation:

    • $p->close(); is never executed because you always return from the sub before you reach that line. From the Net::Ping docs, you don't even need to close when your code is inside a sub.

      If you actually try an ip that does ping on your network, it still comes back with "does not ping." I will remove the close and also try the qq. Thanks!

        My point was that I cannot reproduce your inconsistent results. Your 3 methods return "does not ping" on an ip that I can't access, and they all return "pings" on an ip that I can access.

        By the way, qw vs. qq will make no difference in this case; I'm not sure why the other monk recommended that change.

Re: Ping not working
by karlgoethebier (Abbot) on Mar 11, 2015 at 12:54 UTC

    Try my $ip = qq(172.16.1.2);

    Update: If i run the code you posted with my $ip = qq(66.39.54.27); i get:

    monks>ping.pl return is 0 pings

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      If i run the code you posted with my $ip = qq(66.39.54.27);

      Because there's only one item in the qw// list, that does not make a difference in this case; the list will return its last item.

      use Data::Dumper; my $ip1 = qw(172.16.1.2); my $ip2 = qq(172.16.1.2); my $ip3 = qw(172.16.1.2 172.16.1.3); my $ip4 = qq(172.16.1.2 172.16.1.3); print Dumper($ip1, $ip2, $ip3, $ip4); __END__ $VAR1 = '172.16.1.2'; $VAR2 = '172.16.1.2'; Useless use of a constant ("172.16.1.2") in void context at - line 4. $VAR3 = '172.16.1.3'; $VAR4 = '172.16.1.2 172.16.1.3';

      (toolic already pointed this out a little while ago.)

        "...toolic already pointed this out a little while ago."

        No. He wrote that he isn't sure. But perhaps i posted to fast, may be :-(

        But i'll take a look at what i did...

        Update: I tried the qq as well as the qw version on my Mac and it seems that both ping. I remember that i tried the same stuff on XP at noon but the qw version didn't work. But may be i miss something. Unfortunately i can't reproduce this on XP tonight. But i'll be back.

        And please note, when i concede a possible fault i did (because of answering to fast for some reason or what ever) i consider even one down vote as extremely impolite.

        Regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Re: Ping not working
by Anonymous Monk on Mar 11, 2015 at 15:04 UTC

    I haven't used ping in a while, and I can't say whether this is the source of your problem, but I remember reading that trying to send a ping from your program/user (as with Net::Ping) is not always reliable because it might actually require root privileges. Net::Ping::External is a workaround that calls an external ping program.

    use Net::Ping::External qw(ping); my $alive = ping(host => "www.perlmonks.org"); print "alive: $alive\n";

    Might be more reliable on those OSes supported by Net::Ping::External.