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

Greetings all, I find that my Net::Ping (Which is reported to be the (latest release) just doesn't seem to work. There was a thread about this a while back, but there was no real solution - so I'm hoping to further the line;
I run Linux 2.2.6 (Slackware) w/5.005_03.

The examples state that you can just pass it a hostname (the Perl Cookbook uses "kingkong.com") which perl responds with:
Bad arg length for Socket::unpack_sockaddr_in, length is 0, should be 16 at /usr/lib/perl5/i386-linux/Socket.pm line 295.

I figure that its expected a packed address, but when I feed it something like (And yes, i'm running as root):

$p = Net::Ping->new('icmp'); print "She's workin" if $p->ping(inet_aton("addr.com")); $p->close();
The script takes 1/4 of a second to complete, making me very suspicious as to whether anything is happened at all! Increasing the timeout does nothing, and the command still runs for 1/4 of a second.
Anyone faced and overcome this?
JP

Replies are listed 'Best First'.
Re: Net::Ping just don't werk
by brother ab (Scribe) on Nov 02, 2000 at 13:39 UTC

    Really, there are two problems - why host is unreachable and why "Bad arg length" error appears.

    You could read the discussion of both the problems here. Solution is in Msg5.

    For impatient:

    Host is unreachable because Net::Ping being used with UDP or TCP checks it via echo service (port 7). This service is down by default on some linux and BSD (one man said me that it is due to security reason). Solution: use ICMP (and run as root) or switch on echo service on remote host.

    "Bad arg length" error appears if remote host is unreachable (may be it is the result of the first problem). Simple patch to Net::Ping - while waiting package you should check return value of recv and do nothing if it is false.

    Patch for Net::Ping 2.02 - change line 373

    $from_saddr = recv($self->{"fh"}, $from_msg, 1500, $flags);

    to

    $from_saddr = recv($self->{"fh"}, $from_msg, 1500, $flags) or last;

    and you will have remote host unreachable instead of the error.

    Could anybody tell me - where and how should I officially submit this patch?

    -- brother ab
Re: Net::Ping just don't werk
by moen (Hermit) on Nov 02, 2000 at 04:42 UTC
    As he says he is running as root I presume that Linux/Unix is the test case, so i did this:
    #!/usr/bin/perl use Net::Ping; $p = Net::Ping->new('icmp'); print "She's workin\n" if $p->ping("ns.online.no"); $p->close();
    Prints:
    zodiac:/home/perl# ./test1.pl She's workin zodiac:/home/perl#
    So it works like a charm running perl version 5.005_03 on kernel 2.2.17 (Debian potato).
    Don't know about that inet_aton thingy though..?
Re: Net::Ping just don't werk
by JPaul (Hermit) on Nov 02, 2000 at 05:16 UTC
    I was more than surprised to say the least - check this:
    #!/usr/bin/perl use Net::Ping; $p = Net::Ping->new('icmp'); print "She's workin\n" if $p->ping("ns.online.no"); $p->close();
    Works fine! HOWEVER
    #!/usr/bin/perl use Net::Ping; $p = Net::Ping->new(); print "She's workin\n" if $p->ping("ns.online.no"); $p->close();
    Will not!
    JP
      i ran into this problem a while back.

      Net::Ping requires UID 0 ( as already noted ) to run ICMP pings.

      the other services ( TCP and UDP ) need to be implemented and accessible on the receiving host. the default proto is UDP according to the docs.

Re: Net::Ping just don't werk
by mitd (Curate) on Nov 02, 2000 at 12:44 UTC
    A couple of things:
    1. Read docs carefully.
    2. icmp MAY need suid 0 , udp and tcp don't.
    3. tcp just connects no data is transfered or echo'ed, so if you just want test if target host is alive use tcp.
    4. Your example is little vague on target address. Make sure your target is a host, not a domain. Not all domains, (ie foo.com) have corresponding hosts (ie yahoo.com DOES, kingkong.com DOES NOT).
    5. Net::Ping will accept fully qualified host nmaes and IP numbers they do not require packing
    6. Suggest using IP numbers first until you have it working. Try good old ping('127.0.0.1')

    mitd-Made in the Dark
    'My favourite colour appears to be grey.'

      Update Great answer brother ab which leads me to a clarification of my point 3. tcp just connects...

      Yes tcp does just connect and does not transfer data but connect is through tcp echo service port so, as brother ab points out, if tcp echo service is turned off in inetd.conf we is poop out of luck.

      Again, thanks brother ab for clearing some past mysteries for me as well. Your answer got a ++ vote from moi ! :)

      mitd-Made in the Dark
      'My favourite colour appears to be grey.'

Re: Net::Ping just don't werk
by elwarren (Priest) on Nov 02, 2000 at 05:33 UTC
    It works for me on NT and Linux. Check it out over on this node.
    #!/usr/bin/perl @pinglist=("www.yahoo.com", "www.thisisntreal.com", "wlindsey.bbcg.com +"); use Net::Ping; sub test_ping; foreach $h (@pinglist) { if (test_ping($h)) { print "[p (ok) $h]"; } else { print "[p <NO> +$h]"; } print "\n"; } sub test_ping { # test_ping($hostname) my $hostname=shift; my $p=Net::Ping->new(icmp,5); return $p->ping($h,5); $p->close; } # end test_ping
Re: Net::Ping just don't werk
by Anonymous Monk on Nov 02, 2000 at 04:34 UTC
    I'm using the ActiveState 5.6 on Win98 and Net::Ping doesn't seem to work either: It 'seems' that if the ping target responds the first time then the ping sub returns true, and if the ping target doesn't respond the first time then the ping sub always returns false, even if the ping target starts responding. I have not tested this out fully so I may be incorrect about this but I know one thing: It was funky so I stopped using it.
Re: Net::Ping just don't werk
by brick (Sexton) on Nov 02, 2000 at 06:14 UTC
    Curiosity question here:
    Are you behind some sort of firewall that would be
    blocking the traffic based on port/protocol combos
    or limitations? That's the first thing that springs
    to mind when you mention the fact that it will work
    on one but not the other. I haven't read the module
    or the function so I don't know if a specific port is
    used as a default or if you have the option of defining
    one, but that might be a place to start looking for a
    culprit.

    -brick.
Re: Net::Ping just don't werk
by JPaul (Hermit) on Nov 02, 2000 at 05:36 UTC
    But you're using icmp, which I just said does work - its when it uses tcp/udp that it won't...

    JP