0

I am trying to echo out the the average round trip time for 4 ICMP echo/echo rely packets (in ms) for a website like google. This is my code as of now.

echo "$(ping -c 4 google.com | grep '??????')"

Pinging the website works but I have no idea how to echo out only the average round trip time. I have only used Regex for validation on web forms, but I haven't used it in awhile. I assume I can use Regex to find only what I am searching for, but if there is a better way of doing this, that would also be great. I am writing this using shell scripting for linux ubuntu

This is an example of the output. The only part of this I need is the part at the bottom where it says rtt min/avg/max/mdev = 14.556/14.579/14.614/0.088 ms.

PING google.com (142.250.74.238) 56(84) bytes of data.
64 bytes from par10s40-in-f14.1e100.net (142.250.74.238): icmp_seq=1 ttl=108 tim                                                                                                                                                                             e=14.5 ms
64 bytes from par10s40-in-f14.1e100.net (142.250.74.238): icmp_seq=2 ttl=108 tim                                                                                                                                                                             e=14.5 ms
64 bytes from par10s40-in-f14.1e100.net (142.250.74.238): icmp_seq=3 ttl=108 time=14.5 ms
64 bytes from par10s40-in-f14.1e100.net (142.250.74.238): icmp_seq=4 ttl=108 time=14.6 ms

--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 14.556/14.579/14.614/0.088 ms
2
  • What's the ping output look like? Commented Dec 12, 2020 at 1:04
  • ill edit the question. the message is too long Commented Dec 12, 2020 at 1:09

1 Answer 1

1

Assuming the ping output looks like the following:

$ ping -c 4 google.com
PING google.com (172.217.14.238): 56 data bytes
64 bytes from 172.217.14.238: icmp_seq=0 ttl=118 time=78.019 ms
64 bytes from 172.217.14.238: icmp_seq=1 ttl=118 time=62.416 ms
64 bytes from 172.217.14.238: icmp_seq=2 ttl=118 time=63.019 ms
64 bytes from 172.217.14.238: icmp_seq=3 ttl=118 time=62.415 ms
--- google.com ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max/stddev = 62.415/66.467/78.019/6.674 ms

In this scenario the average time is 66.467 (ms)

One awk solution:

awk '/avg/ {split($0,arr,"/"); print arr[5]}'

Where:

  • /avg/ - look for line with the string avg
  • split($0,arr,"/") - split the line using a forward slash (/) as the delimiter, place the segments in the arr[] array
  • print arr[5] - print the 5th element of the arr[] array

Combining with the ping:

ping -c 4 google.com | awk '/avg/ {split($0,arr,"/"); print arr[5]}'
66.467

And if we need to include the measurement of time (ms in this case), we can also print the last field of the line that contains the string avg, eg:

ping -c 4 google.com | awk '/avg/ {split($0,arr,"/"); print arr[5],$NF}'
66.467 ms

NOTES:

  • OP may need to tweak the awk command if their ping output is in a different format
  • obviously (?) each time the ping is run we're likely to get a slightly different value.
  • if OP wants to be 100% certain of the value then the ping command output should be saved to a file (or variable) and then run the awk command against said file (or variable)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.