Question

I'm currently trying to write a bash script to log the average ping time of atm. three different targets, every 20 seconds for a whole day. This is what I currently have..

#!/bin/bash

echo "SCRIPT STARTED" >> pingthing.log
date +%d.%m.%y' '%R:%S >> pingthing.log

for i in $(seq 1 4320);
do
    date +%d.%m.%y' '%R:%S >> pingthing.log
    #save just target IP and avg time.
    ping -c 3 -q -W 2 8.8.8.8 >> pingthing.log
    ping -c 3 -q -W 2 64.25.40.16 >> pingthing.log
    ping -c 3 -q -W 2 96.17.199.48 >> pingthing.log
    sleep 20
done

echo "SCRIPT ENDED" >> pingthing.log
date +%d.%m.%y' '%R:%S >> pingthing.log

Now to my question...

  • How to sed/awk the ping summary to just save the target and the avg time?
  • and how could I handle a 100% loss case?

EDIT: sorry, I have no experience with sed/awk but know that it can be done with these tools.. let me try to clarify myself

This is currently what is saved in my logfile.. yet this only shows the very start of it

SCRIPT STARTED
07.02.14 22:14:13
07.02.14 22:14:13
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.

--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 127.773/152.321/192.204/28.452 ms
PING 64.25.40.16 (64.25.40.16) 56(84) bytes of data.

--- 64.25.40.16 ping statistics ---
3 packets transmitted, 3 received, +2 duplicates, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 213.889/237.182/286.825/26.656 ms
PING 96.17.199.48 (96.17.199.48) 56(84) bytes of data.

--- 96.17.199.48 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 305.028/340.081/375.135/35.058 ms

now I'd want to have only the target and its avg. time of the ping command like

SCRIPT STARTED
07.02.14 22:14:13
07.02.14 22:14:13
8.8.8.8 152.321
64.25.40.16 237.182
96.17.199.48 340.081

I'm aware that I should pipe the ping command to sed/awk but as I have no experience with this I left it out for the time being.

I wouldn't want you to just solve everything, I'm here to discuss and learn. For the 100% loss problem.. the output would look like this

ping -W 2 -q -c 3 1.1.1.1

PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.

--- 1.1.1.1 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 2016ms

Now here is no summary line as in the ones with successful ping, so I can't use sed/awk for that pattern..

Was it helpful?

Solution

Given your posted sample input file:

$ awk -F'[ /]' 'NR~/^[123]$/; /^---/{ip=$2} /^rtt/{print ip, $8}' file
SCRIPT STARTED
07.02.14 22:14:13
07.02.14 22:14:13
8.8.8.8 152.321
64.25.40.16 237.182
96.17.199.48 340.081

You don't tell us what output you want for "the 100% loss problem" so I don't know what you want done with that. Just include it in your sample input and expected output unless there's some specific reason not to that isn't clear so far.

If all you want is something printed stating 100% loss, you could just tweak the script to:

awk -F'[ /]' 'NR~/^[123]$/; /^---/{ip=$2} /^rtt/{print ip, $8} /100% packet loss/{print ip, "100% packet loss"}' file

The possibilities are endless... just tell us what you need to be output.

Here it is one line at a time with comments:

awk -F'[ /]' '       # use space and / as the field separator
NR~/^[123]$/;        # if youre on input line 1, 2, or 3, print that line (the default action)
/^---/{ip=$2}        # if the line starts with 3 dashes, save the 2nd field as the IP address
/^rtt/{print ip, $8} # if the line starts with rtt, print the saved IP address and the 8th field which is the averages
/100% packet loss/{print ip, 2000} # if the line contains the 100%... statement, print the IP address and a default value of 2000
' file
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top