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

Hey everyone. I'm writing a script that uses rsync with ssh to sync files with a bunch of boxes. I want it to check if the clients are up before attempting to rsync, so I used Net::Ping.

Trouble is, Net::Ping returns false even if the host is up. Here is a snippet of test code I used.
#!/usr/local/bin/perl -w use Net::Ping; $host = "138.238.227.90"; $p = Net::Ping->new(); print "$host is alive.\n" if $p->ping($host); $p->close();
When I run this, I get no return, no matter what the host state. And this is directly from the Net::Ping docs.
I'm on an Ultra10 running Solaris 7. Whats more is that when I tried to test it out on a Linux box (2.2.12-20smp) I get this:
Use of uninitialized value at /usr/lib/perl5/5.00503/i386-linux/Socket +.pm line 295. Bad arg length for Socket::unpack_sockaddr_in, length is 0, should be +16 at /usr/lib/perl5/5.00503/i386-linux/Socket.pm line 295.

Perl version on Linux is 5.005_03
Perl version on Solaris is 5.005_03
Net::Ping version on both is 2.02

If anyone has any suggestions, including an alternative to Net::Ping, I would be most grateful.

Replies are listed 'Best First'.
Re: Trouble with Net::Ping
by the_slycer (Chaplain) on Jan 31, 2001 at 21:57 UTC
    All I can tell you is that it's the same error on Windows 2000 Pro with same code.

    Update: I tried changing the protocols around and came up with these results:
    $p = Net::Ping->new("udp");
    Same error as above
    $p = Net::Ping->new("tcp");
    The Unsupported function alarm function is unimplemented at C:/Perl/lib/Net/Ping.pm line 308.
    $p = Net::Ping->new("icmp");
    This last one worked perfectly..
    HTH
      ICMP works perfectly, thanks for the info.
      Unfortunately, I believe sending ICMP packets requires root privs, which can
      make things difficult if you arent running your scripts as root or SUID.

      I think Ill mail the guy who wrote Net::Ping and get his input on it
Re: Trouble with Net::Ping
by isotope (Deacon) on Jan 31, 2001 at 22:09 UTC
    Try some test cases with this... I ran it on my Ultra 10 and got some interesting results. I tried pinging localhost, the assigned IP of my box, and a physically adjacent machine on a different subnet...
    #!/usr/local/bin/perl -w use Net::Ping; use Data::Dumper; for(@ARGV) { $p = Net::Ping->new(); $return = $p->ping($_); $p->close(); print $_ . "\n"; print Data::Dumper->Dump([$return], [qw(*$_)]); }

    --isotope
    http://www.skylab.org/~isotope/
Re: Trouble with Net::Ping
by Brother Joe (Acolyte) on Feb 01, 2001 at 04:16 UTC
    Heya, I too encountered this when using Net::Ping. Specifing a proto of "udp" causes Net::Ping to send 1 byte to the echo port on the remote machine. Specifying a proto of "tcp" also does this. Echo is a udp/tcp service that runs out of inetd a quick grep of /etc/inetd.conf should show whether or not you've got it flipped on. Windows boxes appear to cause a non-response (just like hosts with out echo turned on)... In the end I used ICMP for my proto but then again it was a quick hack and I'm root... So, unless you have echo turned on, or you want to run as root Net::Ping may not do what you need. Might I suggest the following uglyness: (it does have the added bonus of letting you know if the $host has sshd running..)
    #!/usr/bin/perl -w
    
    use IO::Socket;
    
    $host = '192.34.34.1';
    $port = '22';
    
    $socket = IO::Socket::INET->new
    (
      PeerAddr => $host,
      PeerPort => $port,
      Proto => "tcp",
      Type => SOCK_STREAM,
      Timeout => 2
    ) or die "Can't open port dude..\n";
    
    $answer = <$socket>;
    
    close($socket);
    
     print "Host is alive\n" if($answer);
    
Re: Trouble with Net::Ping
by mr.nick (Chaplain) on Jan 31, 2001 at 22:07 UTC
    I'm afraid that I get the same error with my Linux system also (Perl 5.005_03). When I mentioned it here I was told to upgrade Net::Ping; which was already at it's latest version.

    I tried upgrading related packages such as IO::Socket and friends to no avail.

    After I commented on that, I was told to debug it myself. Unfortunately, my debugging skills weren't up-to-snuff, so I was never able to resolve it.

    If you discover the fix, please make sure you post it around here someplace; there are others that would benefit from hearing it.

Re: Trouble with Net::Ping
by Dragonfly (Priest) on Feb 01, 2001 at 06:28 UTC

    After reading through the comments in this thread it is striking to me how many people out there have had problems using Net::Ping. Tye's post and link to the previous discussion also seems to indicate that numerous workarounds have been needed to correctly ping from within Perl.

    I also recently had this problem, last week in fact, when I had to write a quickie to scan my subnet for live boxes, and I resolved it somewhat unsatisfactorily by running my script from a Windows 98 box using ICMP. This is fine but I would have preferred to run the script as a nonprivileged user on my BSD box.

    I'd like to see it get patched, perhaps, but I don't know enough to figure it out myself. *chuckle*

    Update: I bought a couple C and C++ books, so perhaps one day I will be ready to take a look at the module. Someday...