in reply to MRTG script should be easy

If you know the exact context of the data you want (which is a pretty reasonable assumption for ping's summary info), you can use a m// match to extract the info you want. In my opinion, using split is the wrong way to do this.
#!/usr/bin/perl my $host = 'dslreports.com'; my $ping_data = `ping -c4 $host`; my ($sent, $rev, $lost, $min, $max, $avg); if ( $ping_data =~ /(\d+) packets transmitted, (\d+) packets received, (\d+)%/ + ) { ($sent, $recv, $lost) = ($1, $2, $3); print "\$sent=$sent, \$recv=$recv, \$lost=$lost\n"; } if ( $ping_data =~ m{round-trip min/avg/max = ([0-9\.]+)/([0-9\.]+)/([0-9\.]+) +} ) { ($min, $avg, $max) = ($1, $2, $3); print "\$min=$min, \$avg=$avg, \$max=$max\n"; } ## do what you will with those variables now...
On my system, this outputs:
$sent=4, $recv=4, $lost=0 $min=47.0, $avg=47.6, $max=48.4
You should probably read this section of perlop pertaining to the m// operator.

PS: I admit those long regexes could be broken up, simplified, /x'ed, etc, but hopefully you get the point! Update: no more wrapping within the <code>, at least under my settings.

blokhead

Replies are listed 'Best First'.
Re: Re: MRTG script should be easy
by Tanalis (Curate) on May 20, 2003 at 07:54 UTC
    I'd have to disagree with using m// to grab data out of a system ping.

    Using a regex to grab out the data would restrict the script to running on one OS and one version of ping - as has been pointed out already, ping outputs slightly different data between versions (from a "Host is Alive" on Solaris to timings, averages and losses on my linux system at home). Changes to either the OS or the version of ping could, and probably would, break the regex, and the script would fail.

    Having said that, though, splitting as the OP is currently doing would restrict to an OS/version in a similar way, so I guess nothing would be lost by using a regexp from his PoV.

    -- Foxcub
    #include www.liquidfusion.org.uk