Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Net::Ping duration question

by Anonymous Monk
on Jun 21, 2012 at 05:13 UTC ( #977557=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I will make this quick. I'm following the example right out of the description page.
while ( 1 ) { my $ret; my $duration; my $ip; ( $ret, $duration, $ip ) = $p->ping( $host, 5.5 ); printf( "$host [ip: $ip] is alive (packet return time: %.2f ms +)\n", 1000 * $duration ); }
The result I get back for $duration * 1000 is either 0 or 1000 ms. The problem is, the host I'm using has an average ping time of 127ms, so something just isn't adding up. Am I missing something?

Comment on Net::Ping duration question
Download Code
Re: Net::Ping duration question
by frozenwithjoy (Curate) on Jun 21, 2012 at 05:29 UTC
    Which version of perl do you have? More specifically, is Time::HiRes part of your core? It should be unless you are on a Commodore64 or something else from my childhood...

    If so, this should fix your problem: $p->hires();. Put it right after $p = Net::Ping->new();

      That did it, thanks. I got no error so assumed Time:HiRes was there, but didn't see I needed to make that call. All bueno now!
        Perfect! By the way, if you print out $ret, what do you get? I keep getting 0, even though I'm getting realistic duration values. I was under the impression that I'd get $ret == 1 if the host was reachable.
Re: Net::Ping duration question
by NetWallah (Abbot) on Jun 21, 2012 at 05:34 UTC
    Try setting
    $p->hires(1);
    before the ping call. This will cause Net::Ping to use Time::HiRes, and return a Float, instead of using normal (1 second) resolution, and returning an Integer.

    Of course, this requires Time::HiRes to be installed/available.

    Update: Dang - frozenwithjoy (++) has a higher Res, and beat me to the punch.

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

      Come on up here with me. There's plenty of glory to go around and no one I'd rather share it with more than you!

      edit: Oh noes! down-voted! I guess I better stop enjoying myself while I'm spending my time helping people out. ^___^

        Its past my bedtime now, so I'll let you bask!. Thanks for the invite, and Cheers!
        We'll play again on the morrow.

                     I hope life isn't a big joke, because I don't get it.
                           -SNL

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (18)
As of 2015-07-06 17:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (79 votes), past polls