Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Unexpected results from ping

by GreyOwl (Initiate)
on Mar 04, 2003 at 14:47 UTC ( [id://240363]=perlquestion: print w/replies, xml ) Need Help??

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

What's going wrong here?

#!/usr/bin/perl5 -w use Net::Ping; my @pinglist = ( "svhappy" ,"w01b0rji" ,"w01b0rji.cube.fred.ch" ,"svmoloch.flur.fred.ch" ,"160.69.125.142" ); $p = Net::Ping->new(); foreach $h (@pinglist) { chomp (my $res = `ping $h`); if ($res) {print "$h \`ping\` OK :\t$res\n"} else {print "$h \`ping\` NOK\n"} if ($p->ping($h)) {print "$h \$p->ping OK\n"} else {print "$h \$p->ping NOK\n"} print "\n"; } $p->close();

...gives following results:

(svmoloch:szhxrv) $ ./test.pl

ping: unknown host svhappy
svhappy `ping` NOK
svhappy $p->ping NOK

ping: unknown host w01b0rji
w01b0rji `ping` NOK
w01b0rji $p->ping NOK

w01b0rji.cube.fred.ch `ping` OK : w01b0rji.cube.fred.ch is alive
w01b0rji.cube.fred.ch $p->ping NOK

svmoloch.flur.fred.ch `ping` OK : svmoloch.flur.fred.ch is alive
svmoloch.flur.fred.ch $p->ping NOK

160.69.125.142 `ping` OK : 160.69.125.142 is alive
160.69.125.142 $p->ping NOK

Replies are listed 'Best First'.
Re: Unexpected results from ping
by xmath (Hermit) on Mar 04, 2003 at 14:51 UTC
    By default Net::Ping uses a different method of determining a host's status (TCP echo) than the 'ping' utility (ICMP echo) because doing ICMP echo on unix systems requires root privileges to open a so-called "raw socket".

    You can however make Net::Ping use the external ping utility via the Net::Ping::External module if you want to do ICMP echo without running your script as root.

    See the documentation of Net::Ping (note that CPAN has a more recent Net::Ping than the one included with perl 5.8.0)

    •Update: It's still odd however that it doesn't work at all.. even when a TCP echo service isn't running on a host (these days it almost never is) it should still get a "connection refused" which it would interpret as "the host is alive since it can refuse the connection".

    Is it possible there's a firewall in the way that blocks tcp port 7 (echo) ? Try setting the port to 80 by doing:

    $p->{port_num} = 80;
      Yes, that is correct. The ping utility uses ICMP echo request/ICMP echo reply while, by default, Net::Ping tries to establish a TCP session with the echo port of the target host. Try making this change to your program:
      $p = Net::Ping->new('icmp');
      Update
      w01b0rji.cube.fred.ch `ping` OK : w01b0rji.cube.fred.ch is alive w01b0rji.cube.fred.ch $p->ping NOK svmoloch.flur.fred.ch `ping` OK : svmoloch.flur.fred.ch is alive svmoloch.flur.fred.ch $p->ping NOK 160.69.125.142 `ping` OK : 160.69.125.142 is alive 160.69.125.142 $p->ping NOK
      Just to clarify, what you are seeing here is most likely the result of the specified hosts allowing ICMP echo requests, but not having a service listening to the echo port.
      By default Net::Ping uses a different method of determining a host's status (TCP echo) than the 'ping' utility (ICMP echo) because doing ICMP echo on unix systems requires root privileges to open a so-called "raw socket".

      It does not take root privileges to create an ICMP echo request... you can test it yourself by logging in as a non-root user and playing with the "ping" program. Maybe you have confused this with putting the network card in promiscuous mode which is required for programs like tcpdump?

      Update You know, xmath I never noticed that ping was setuid to root, so that, indeed, was a poor example. *Smiles*

      Update 2 Ok, so I went back through all my crafted packet code, and I can not find a single example of being able to craft an ICMP packet without being root. With that said, it does indeed seem that xmath was correct.

        you can test it yourself by logging in as a non-root user and playing with the "ping" program

        The "ping" utility is always setuid root:

        [Quoose:~] xmath% ls -l `which ping` -r-sr-xr-x 1 root wheel 24920 Dec 23 15:17 /sbin/ping

        That's why normal users can use it. :-)

        Update: it seems that lately raw sockets are no longer needed for ICMP on some platforms (Darwin / Mac OS X); but on most (Linux etc) they still are

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-25 20:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found