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

I'm writing a program that will ping a specified IP. What I need to know is how I can get the max value from the ping. For example, I have the following round trip info:

round-trip min/avg/max/mdev = 55.774/62.750/146.468/16.942 ms

I need to find out if the max value is greater than 500 and if it is greater than 500, do a tracert on the IP and print out the results.

Should I use the Net::Ping or is there a better way? Any help is greatly appreciated. Meanwhile I'll be searching this site and see what I can find.

Thanks!

qball~"I have node idea?!"

Replies are listed 'Best First'.
Re: Ping and Tracert
by marcus (Scribe) on Aug 31, 2001 at 21:47 UTC
    Seems to be Net::Ping doesn't suit you, since it'll only return alive/dead, and you can't set the timeout to less than a second.

    Shouldn't be too hard to $result=`ping $host`; And parse the output with a simple regexp, but you should be aware that ping returns different output depending on which platform you run it on, so if you are going to distribute it, that might not work for you.

    Anyways, good luck :)

    qw[marcus]

      It will run on both Solaris and Linux. Any suggestions?

      Thanks.

      qball~"I have node idea?!"

        Run ping, generate some output and then I will suggest a regex for you to capture the max. This runs under Win32. The output is after the __DATA__ - you can see the output of ping which puts the regex into perspective.

        $ip = 'www.perlmonks.com'; $ping = `ping $ip`; @times = $ping =~ m/time=(\d+)ms/g; @times = sort { $b <=> $a } @times; print "High to low @times ms\n"; print "Highest $times[0] ms\n"; print "Lowest $times[-1] ms\n"; print "This was the repy parsed\n$ping\n"; __DATA__ High to low 414 413 408 403 ms Highest 414 ms Lowest 403 ms This was the repy parsed Pinging www.perlmonks.com [206.170.14.76] with 32 bytes of data: Reply from 206.170.14.76: bytes=32 time=408ms TTL=237 Reply from 206.170.14.76: bytes=32 time=413ms TTL=237 Reply from 206.170.14.76: bytes=32 time=414ms TTL=237 Reply from 206.170.14.76: bytes=32 time=403ms TTL=237

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        Well, ping on solaris and linux is quite different :/ I don't think solaris supports getting the max value at all other than interactively. I guess an alternative would be compiling GNU perl on the solaris machine, if you aren't going to distribute to a large number of hosts. There is a Net::Ping::External as well, but unfortunately it doesn't give out any more inforation than Net::Ping does.

        qw[marcus]

        Net::Traceroute should do what you need.

        It parses the output of an actual traceroute. I tested it on Linux and it works well. Solaris should work too, the output format is practically the same.

        -- TMTOWTDI

Re: Ping and Tracert
by the_slycer (Chaplain) on Aug 31, 2001 at 22:01 UTC
    Net::Ping kind of sucks - see all the posts on this site about it.

    I would suggest using the system ping command and parse the results. Something like (on my slack box):
    my @res = `ping -c 3 $ip`; if (pop @res =~ /(\d+\.\d+)\/(\d+\.\d+)\/(\d+\.\d+)) { my @sorted = sort ($1,$2,$3); print pop @sorted }
    Update: just for kicks, here's the above golfed down a bit :-)
    print(((sort(((`ping -c3 $ip`)[-1])=~/(\d+\.\d+)\/(\d+\.\d+)\/(\d+\.\d ++)/))[-1]) )
    Also - regarding your question re: multiple OS's. That's the problem with non-modular code. Anyways, what I would suggest is that you set the command you will run earlier in the script depending on the os (ie $ping = "command" if $^O =~ /Solaris/ etc..
      -c 3 doesn't work on Solaris. Any suggestions as to how I can write the script to be usable on both Solaris and Linux platforms?

      Thanks.

      qball~"I have node idea?!"

        You get the OS name in $^O so all you need to do is (pseudocode)

        print "The OS is $^O"; if ( $^O =~ /Linux/ ) { &linux_ping; } elsif ( $^O =~ /Solaris/ ) { &sun_ping; } else { die "Get a real OS!\n"; }

        I don't know what $^O holds on Solaris so you will have to tweak the matches to suit.

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Ping and Tracert
by idnopheq (Chaplain) on Sep 01, 2001 at 16:50 UTC
    Hi, qball!

    See if ping for PPT will help. I have a results portion in there, and it seems relatively accurate compared to Solaris ping. Take a look and see if you can modify it for your own needs.

    HTH
    --
    idnopheq
    Apply yourself to new problems without preparation, develop confidence in your ability to to meet situations as they arrise.

Re: Ping and Tracert
by acid06 (Friar) on Sep 01, 2001 at 16:54 UTC
    Despite all the bad comments about it, you can still use Net::Ping and Time::HiRes for timing...

    acid06
    perl -e "print pack('h*', 16369646), scalar reverse $="