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

I have written the following script: (I meant to use Net-Ping but I didn't have time to read its docs) (and I run this on linux boxes occasionally so that's the shebang and yes I change the -n to a -c)

#!/usr/bin/perl -w use strict ; use diagnostics ; use LWP::Simple ; #this will run a ping to our firewall #to yahoo.com #to 132.163.4.101 an NIST Time Server #and with get the time (UTC) #run this script piped to text from the prompt my $ping1 = (`ping XXX.XXX.XXX.XXX -n 1`) ; my $ping2 = (`ping yahoo.com -n 25`) ; my $ping3 = (`ping 132.163.4.101 -n 25`) ; my $numb = 51 ; until ($numb == 1) { chomp $numb ; $numb -- ; print "R---------------------------\n" ; print $ping1 ; print "Y---------------------------\n" ; print $ping2 ; print "I---------------------------\n" ; print $ping3 ; print "T---------------------------\n" ; getprint "http://132.163.4.101:13 \n" ; print "E---------------------------\n" ; }


It produces this output: (edited for content!)

P--------------------------- Pinging XXX.XXX.XXX.XXX with 32 bytes of data: Reply from XXX.XXX.XXX.XXX: bytes=32 time<10ms TTL=63 Ping statistics for XXX.XXX.XXX.XXX: Packets: Sent = 1, Received = 1, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0ms P--------------------------- Pinging yahoo.com [216.115.108.245] with 32 bytes of data: Reply from 216.115.108.245: bytes=32 time=70ms TTL=237 ... Reply from 216.115.108.245: bytes=32 time=90ms TTL=237 Ping statistics for 216.115.108.245: Packets: Sent = 25, Received = 25, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 70ms, Maximum = 90ms, Average = 72ms P--------------------------- Pinging 132.163.4.101 with 32 bytes of data: Reply from 132.163.4.101: bytes=32 time=90ms TTL=39 ... Reply from 132.163.4.101: bytes=32 time=111ms TTL=39 Ping statistics for 132.163.4.101: Packets: Sent = 25, Received = 25, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 90ms, Maximum = 111ms, Average = 92ms T--------------------------- 52138 01-08-17 22:08:06 50 0 0 404.0 UTC(NIST) * T---------------------------


I would like the final output to look like this:

Reply from XXX.XXX.XXX.XXX: bytes=32 time<10ms TTL=63 ------------------------------------------------------- Reply from 216.115.108.245: bytes=32 time=70ms TTL=237 ... Reply from 216.115.108.245: bytes=32 time=90ms TTL=237 Average = 72ms ------------------------------------------------------- Reply from 132.163.4.101: bytes=32 time=90ms TTL=39 ... Reply from 132.163.4.101: bytes=32 time=111ms TTL=39 Average = 92ms ------------------------------------------------------- 52138 01-08-17 22:08:06 50 0 0 404.0 UTC(NIST) * -------------------------------------------------------


I pipe the output to text in a file called (imaginatively) ping.txt so I am thinking that I should write another script to pull the info I want out. Or possibly do some sort of manipulation at the end of the until loop? This is one of those case where I know exactly what I want and have NO IDEA how to get it. any ideas? pointers? perldocs? how would you do it?
--
lmoran@wtsgSPAM.com
print "\x{263a}"

Replies are listed 'Best First'.
Re: Getting the text I want
by Masem (Monsignor) on Aug 21, 2001 at 18:18 UTC
    I would break each of your ping strings up via split'ing on newlines, as a starting point. Within each new array, first grep out only those that start with "Reply" into a second array. Then, assuming that the average will always be in the last one, you can grab that portion from the last element of the split.

    Something like the following:

    my @pings = split /\n/, $ping; my @keep = grep { /^Reply/ } @pings; $pings[-1] =~ /(Average =.+)$/ ); my $avg = $1; push @keep, $avg; print join '\n', @keep;
    Other transformations are then rather trivial.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    It's not what you know, but knowing how to find it if you don't know that's important

      I would break each of your ping strings up via split'ing on newlines, as a starting point.
      Isn't it easier in this case to use an array directly to get the output from the backticks? my @ping1 = `ping some.one.org`;

      -- Hofmator

Re: Getting the text I want
by dragonchild (Archbishop) on Aug 21, 2001 at 18:18 UTC
    Ok. Let's take a moment to think. You have the complete text from the first ping in $ping1. There are a number of carriage-returns, so you could, if you wanted to manipulate the lines in $ping1, do something like:
    my @ping1 = split /\n/, $ping1;
    At this point, you have all the lines. Apparently, you want to skip the first 3 lines and the last 5 lines. But, if there is an average, you want to grab that, as well. So, maybe do something like:
    my @ping = split /\n/, $ping; if (@ping > 9) { # Get the average using regex/split/whatever. } splice @ping, -5; splice @ping, 0, 3; print "\n"; print "$_\n" foreach @ping; print "\n";
    That should work, with a little finagling. I didn't test it and I figured you'll probably work with it a little.

    ------
    /me wants to be the brightest bulb in the chandelier!

    Vote paco for President!

Re: Getting the text I want
by MZSanford (Curate) on Aug 21, 2001 at 19:38 UTC
    just because it hasen't been said, reading the Net::Ping documentation and rewritting using that may actually be quicker than all of code changing/testing/changing/testing/ad infinitum ...
    But, if not Net::FTP, i think the other posts here have the answers ... just thought i should re-iterate
    can't sleep clawns will eat me
    -- MZSanford