Dear monks. I have a large ping result output like following:

[root@smokeping ping_analisis]# ping -c3 8.8.8.8; ping -c3 8.8.4.4 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8: icmp_seq=1 ttl=117 time=5.92 ms 64 bytes from 8.8.8.8: icmp_seq=2 ttl=117 time=6.76 ms 64 bytes from 8.8.8.8: icmp_seq=3 ttl=117 time=5.87 ms --- 8.8.8.8 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 5.873/6.184/6.760/0.417 ms PING 8.8.4.4 (8.8.4.4) 56(84) bytes of data. 64 bytes from 8.8.4.4: icmp_seq=1 ttl=116 time=8.56 ms 64 bytes from 8.8.4.4: icmp_seq=2 ttl=116 time=8.46 ms 64 bytes from 8.8.4.4: icmp_seq=3 ttl=116 time=8.71 ms --- 8.8.4.4 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2006ms rtt min/avg/max/mdev = 8.460/8.578/8.715/0.129 ms

I need to map every host I ping with each result.

This is my code

#!/usr/bin/perl @hosts=("8.8.8.8","8.8.4.4"); foreach(@hosts){ $command .= "ping -c 3 $_; "; } my @lines = qx/$command/; foreach(@lines){ if(/PING/){ foreach $host(@hosts){ if(/PING ($host).*/){ $host_key = $host; } } }elsif(/rtt min\/avg\/max\/mdev = (.*)\/(.*)\/(.*)\/(. +*) .*/){ print "$host_key min:$1 avg:$2 max:$3 mdev:$4\ +n"; } }

Even my code is working , I would like to share it since this is a common task and there is probably a better way to do it.

Hope to hear some feedback.

Thanks again,

Leo.


In reply to Create a map from multiple ping output by leostereo

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.