Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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:


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?

Replies are listed 'Best First'.
Re: Net::Ping duration question
by frozenwithjoy (Priest) 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
    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.

      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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://977557]
Approved by kcott
[erix]: I like the idea of the trumps troup of imbeciles growing slightly nervous now their incompetence will be tested
[erix]: you seem forget there is another idiot coming :)
[Your Mother]: The US government does not attract talent, but grafters and liars and it has been that way for the better part of a century. FEW exceptions.
[erix]: you think it only /looks/ worse this time?
[Your Mother]: HR Clinton was literally threatening Russia. She would have been MUCH more likely to start real trouble.
[Your Mother]: I think there is less decorum this time.
[Your Mother]: We’ve had rapists and thieves and unqualified government. They just had decorum.
[erix]: heh, understatement of the week :)
[erix]: well, I hope you're right
[Your Mother]: Bill Clinton assaulted and raped repeatedly. Even when it was hinted and finally all but proved, no one cared because his sexism, and racism, and killing was decorous.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2017-01-20 01:01 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (173 votes). Check out past polls.