Let's see.... could a module do the trick? A search on ping on search.cpan.org returns... Net::Ping! This should both give you a convenient way to get the data in Perl, without having to resort to parsing the output of an external program, and solve portability problems if/when you want to use this tool in a different environment.

After installing Net::Ping, a quick glance at the docs, installing Time::Hires, here is a (tested) piece of code that should work:

#!/usr/bin/perl -w use strict; use Net::Ping; my $NB_PING=5; my $host="dslreports.com"; my( $ok, $nok, $total_time)=(0,0,0); # use the icmp protocol to emulate the unix ping # note that this implies that the script should be # run as root (or suid root) my $p = Net::Ping->new( "icmp"); $p->hires(); # needs Time::Hires foreach (1..$NB_PING) { my ($ret, $duration, $ip) = $p->ping($host, 3); # 3 is the time ou +t in sec if( $ret) { $ok++; $total_time+= $duration * 1000; } else { $nok++; } } $p->close(); # we don't want division by 0 errors do we? my $average= $ok ? int($total_time / $ok) : 0; my $lost= int ( ($nok / ( $ok + $nok)) * 100); print "$lost%\n$average\n0\n0\n";

In reply to Re: MRTG script should be easy by mirod
in thread MRTG script should be easy by jalspach

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.