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

I'm new to perl, and I've recently discovered the use of Net::Ping.

Trouble with Net::Ping is it seems that you can only get it to return a reachable or unreachable status, not a response time in milliseconds like a traditional UNIX ping program.

I'm aware of the use of Net::Ping::External, and of using a UNIX command inside a perl script, but I wanted to know if Net::Ping was capable of outputting response time in milliseconds before resorting to these other methods.

Can someone help?

Rgds,
protos

Replies are listed 'Best First'.
Re: Net::Ping -- other output formats?
by Falkkin (Chaplain) on Apr 19, 2001 at 00:14 UTC
    Short answer: for now, no.

    Long answer: Eventually Net::Ping will be extended in some way (either by parsing results of Net::Ping::External, or perhaps through a new module, Net::Ping::HiRes, to allow for exactly the functionality you want. Look for this functionality in a couple of months... I'm working on it. :)

      Can't you just use Time::HiRes and calculate how long it takes for the ping() method call to take? I realize you'll get a bit more overhead in the calculation than if the module did it for you, but it won't really add up to that much, will it?

      I guess hostname-to-IP lookup could be a problem so you might want to do that "by hand" before calling ping()?

              - tye (but my friends call me "Tye")
Re: Net::Ping -- other output formats?
by Starky (Chaplain) on Apr 19, 2001 at 08:34 UTC
    I can't give you the answer, but I can rule out an attractive possibility for you.

    I tried to wrap the call to $p->ping($host) with some benchmarking by Time::HiRes.

    For example,

    use Net::Ping; use Time::HiRes qw( usleep ualarm gettimeofday tv_interval ); my $p = new Net::Ping; my $t0 = [gettimeofday]; $p->ping('www.yahoo.com'); print "Elapsed time: ", tv_interval ( $t0, [gettimeofday]), "\n";
    Unfortunately, the times I got back were not consistent with the ping user call:
    [bogus@www bin]$ ping -c 1 www.yahoo.com PING www.yahoo.akadns.net (216.32.74.50) from 216.90.131.2 : 56(84) by +tes of data. 64 bytes from www1.dcx.yahoo.com (216.32.74.50): icmp_seq=0 ttl=242 ti +me=290.891 msec --- www.yahoo.akadns.net ping statistics --- 1 packets transmitted, 1 packets received, 0% packet loss round-trip min/avg/max/mdev = 290.891/290.891/290.891/0.000 ms [bogus@www bin]$ ./ping-test.pl Elapsed time: 5.04157 [bogus@www bin]$ ping -c 1 www.yahoo.com PING www.yahoo.akadns.net (216.32.74.52) from 216.90.131.2 : 56(84) by +tes of data. 64 bytes from www3.dcx.yahoo.com (216.32.74.52): icmp_seq=0 ttl=242 ti +me=94.607 msec --- www.yahoo.akadns.net ping statistics --- 1 packets transmitted, 1 packets received, 0% packet loss round-trip min/avg/max/mdev = 94.607/94.607/94.607/0.000 ms [olsonco@www bin]$ ./ping-test.pl Elapsed time: 5.037361 [bogus@www bin]$ ping -c 1 www.yahoo.com PING www.yahoo.akadns.net (64.58.76.178) from 216.90.131.2 : 56(84) by +tes of data. 64 bytes from www9.dcx.yahoo.com (64.58.76.178): icmp_seq=0 ttl=242 ti +me=101.953 msec --- www.yahoo.akadns.net ping statistics --- 1 packets transmitted, 1 packets received, 0% packet loss round-trip min/avg/max/mdev = 101.953/101.953/101.953/0.000 ms [bogus@www bin]$
    It doesn't appear in the perldocs of Net::Ping that it is sending multiple packets, so I'm somewhat mystified where the time difference comes in.

    2001-04-19 Edit by Corion : Added CODE tags

Re: Net::Ping -- other output formats?
by nardo (Friar) on Apr 19, 2001 at 09:21 UTC
    Starky's response is correct, the discrepancy between the ping program and Net::Ping is because Net::Ping uses udp by default, while the system ping uses icmp. If you create your Net::Ping object with Net::Ping->new('icmp') you will get results that agree more closely with the system ping command (note that on unix systems you need to have root privileges to use icmp, which is why ping is suid root). It is still a little slower than the ping binary, presumably due to the ping binary being coded more efficiently and/or being a native executable.
Re: Net::Ping -- other output formats?
by tye (Sage) on Apr 18, 2001 at 21:33 UTC
    This question will be moved to Seekers of Perl Wisdom in a while. protos, if you get a login at the site and use it, then I can send you a private message when stuff like this happens so you'll be able to find your answers more easily.

    Originally posted as a Categorized Answer.