Extract Average Time from Ping -C

extract average time from ping -c

One way is to just add a cut to what you have there.

ping -c 4 www.stackoverflow.com | tail -1| awk '{print $4}' | cut -d '/' -f 2

How to get number of packets received, Average round trip time from ping command if I use popen

I can't think of a BETTER way than extracting the results from the string your read. Exactly the BEST way to parse the string, I'm not sure of [mostly because I haven't spent much time thinking about that!]

An alternative is of course to write your own internal implementation of ping, but I'm fairly sure that is a whole lot more work than using a parser on the popen on the system ping - unless you want VERY portable code [but that'd then also require a very portable networking package, since native networking is not very portable either].

Extract average time using fping

Using awk:

fping -b 12 -c 3 localhost 192.168.0.20 192.168.0.1 192.168.0.18 192.168.0.22 |
awk -F'/' '{print ($8?$8:"-1")}'
0.07
-1
2.57
-1
0.16

Given the / as field delimiter, print the 8th field if it exists otherwise print the string -1

How do you get average RTT of a ping to a certain DNS in Linux using a Bash Script?

steps:

1- do the actual ping: ping -c 10 8.8.4.4

2- get the last line (since the average in the last line) using tail -1, this way it does not matter how much you ping.

3- split the last line by spaces, the forth section is where the average is.

4- split the section and get the actual average.

you can chain the steps in one command in bash using pipes
final result:

ping -c 10 8.8.4.4 | tail -1| awk '{print $4}' | cut -d '/' -f 2

Capturing and averaging ping times

There are almost certainly better ways to get this information, but if you just want to get the average of the output shown above, you could do:

$ awk '$3 ~ /time/ { a+=$4;c++} END{print a/c}' FS== input << EOF
> PING 8.8.8.8 (8.8.8.8): 56 data bytes
> 64 bytes from 8.8.8.8: icmp_seq=0 ttl=54 time=84.222 ms
> 64 bytes from 8.8.8.8: icmp_seq=1 ttl=54 time=82.900 ms
> 64 bytes from 8.8.8.8: icmp_seq=2 ttl=54 time=77.398 ms
> EOF
81.5067

How to get average reply time from ping command?

I would not recommend shelling out to ping.exe and then parsing the output. Use WMI instead:

target = 'somecomputer'
n = 2

Set wmi = GetObject("winmgmts://./root/cimv2")

qry = "SELECT * FROM Win32_PingStatus WHERE address='" & target & "'"

rspTime = 0
cnt = 0
For i = 1 To n
For Each pingStatus In wmi.ExecQuery(qry)
If Not IsNull(pingStatus.StatusCode) Or pingStatus.StatusCode = 0 Then
rspTime = rspTime + pingStatus.ResponseTime
cnt = cnt + 1
End If
Next
Next

If cnt > 0 Then
WScript.Echo "Average response time: " & (rspTime / cnt)
Else
WScript.Echo "Host unreachable"
End If

Bash Script to log average ping times, every 20 seconds for a day

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

Extract substring from string (time from ping reply in C using string library)

Use the standard string.h function strstr to locate a substring inside a string. This returns, when found, a pointer to the first character of the located substring. Add the substring length so its point to the character immediately following that substring.

Then use strtod to convert the ASCII string to a double value. strtod ignores leading whitespace and will use the current locale:

.. leading white-space characters in the string
(as defined by the isspace(3) function) are skipped. The decimal point
character is defined in the program's locale (category LC_NUMERIC).

(from man strtod on OS X)

A robust program should check for invalid input (i.e., no digits follow time=, or the value cannot be represented in a double), but error checking has been omitted here for clarity.

In code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (void)
{
char *input = "64 bytes from 58.27.61.104: icmp_seq=3 ttl=59 time=45.5 ms";
char *looking_for = "time=";
char *pos_of_time;
double the_time;

pos_of_time = strstr (input, looking_for);
if (pos_of_time == NULL)
{
printf ("No match for 'time=' found\n");
} else
{
/* pos_of_time points to the 't' */
/* we need to look beyond the string */
pos_of_time += strlen(looking_for);
the_time = strtod (pos_of_time, NULL);
printf ("time is %f\n", the_time);
}

return 0;
}

which shows the output as expected:

time is 45.500000


Related Topics



Leave a reply



Submit