Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
P is for Practical
 
PerlMonks  

why doesn't Net::ping work when native ping does?

by markseger (Beadle)
on Jan 27, 2012 at 15:38 UTC ( #950391=perlquestion: print w/ replies, xml ) Need Help??
markseger has asked for the wisdom of the Perl Monks concerning the following question:

I had been using Net::Ping for quite awhile as a real simple test to find if a remote machine is alive. Recently I was running that code on a machine that claimed an address wasn't pingable when the /bin/ping said is was.

As an experiment, I created my ping object using 'icmp', and then it worked, but I had to run it as root and I need to do this as a non-priv user. Worst case I can always run /bin/ping inside my perl if I have to but I would much rather find out what is wrong.

use Net::Ping; $host=$ARGV[0]; $p=Net::Ping->new(); print "PING: $host\n"; $status=$p->ping($host); print "STATUS: $status\n";

-mark

Comment on why doesn't Net::ping work when native ping does?
Download Code
Re: why doesn't Net::ping work when native ping does?
by BrowserUk (Pope) on Jan 27, 2012 at 15:47 UTC

    By default, Net::Ping uses the 'tcp' method. Which means it tries to connect to the targets echo port (7). This is frequently disabled.

    The 'icmp' method uses an entirely different mechanism which is less frequently blocked.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: why doesn't Net::ping work when native ping does?
by Eliya (Vicar) on Jan 27, 2012 at 15:59 UTC

    The system ping typically is suid-root

    $ ls -l `which ping` -rwsr-xr-x 1 root root 40048 Dec 3 2008 /bin/ping ^ ^

    which is why it does work (using 'icmp' method), while your unprivileged script does not.  I don't think you're going to have much luck running this unprivileged.

Re: why doesn't Net::ping work when native ping does?
by Corion (Pope) on Jan 27, 2012 at 16:00 UTC
    Your operating system likely does not allow arbitrary users to send ICMP packets. The ping executable is special to your OS, likely it is set up to execute as "root" ("setuid"), which is how it appears that arbitrary users can send iCMP packets with it.
Re: why doesn't Net::ping work when native ping does?
by Anonymous Monk on Jan 27, 2012 at 21:07 UTC
Re: why doesn't Net::ping work when native ping does?
by Khen1950fx (Canon) on Jan 28, 2012 at 15:02 UTC
    LWP::Simple can serve as an alternative to Net::Ping. For example:
    #!/usr/bin/perl -l use strict; use warnings; use LWP::Simple; use Time::HiRes qw(sleep); my $content = head('http://www.perlmonks.com'); foreach (1..20) { if ( $content ) { print "Site is reachable on: ", scalar(localtime()); sleep 0.5; } else { print "Site isn't reachable"; } }
      LWP::Simple can serve as an alternative to Net::Ping.

      What makes you think that every machine runs an HTTP daemon?

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://950391]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2014-04-16 22:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (436 votes), past polls