How to store ping results into an array or variable using a batch file

AI Thread Summary
The discussion centers on executing a ping command to gather latency results and perform statistical calculations on those results using batch scripting. The user seeks to execute "ping google.com -n 200" and store the latency data in a variable for further analysis, such as calculating averages and offsets. There is a suggestion that batch languages may not be suitable for this task due to the complexity of parsing ping output. Instead, using a scripting language like Python or Perl is recommended for better data handling and processing capabilities. The conversation also touches on the possibility of integrating batch files with these scripts for ease of use, especially for daily tasks. Additionally, the use of tools like awk for processing command output is mentioned, highlighting the need for efficient data management without cluttering the system with temporary files. Overall, the consensus leans towards using more advanced scripting languages for the desired functionality.
BiGyElLoWhAt
Gold Member
Messages
1,637
Reaction score
138
TL;DR Summary
I want to ping a site and store the results of all the pings in a variable or array in order to get more descriptive information from the result.
I don't know much about batch, but what I want is to execute a command such as ping google.com -n 200 and store all 200 ping latency results to a variable. The default results (avg max min %lost) isn't enough for me to tell if I can do what I need to do. I would like to calculate the average, offset the average (shift it by (x - avg)) and then calculate the new average of everything > 0. Sort of like stdev for my ping results. I could also rms(result - avg) and get similar information, but I'm not sure how to copy these values without doing it by hand. I would ideally just have a batch file on my desktop that I can double click and have it run and echo the results.
 
Technology news on Phys.org
Responding at a level below your request, have you considered using network diagnostic tools that supply more information than ping ?

In computing, traceroute and tracert are computer network diagnostic commands for displaying the route (path) and measuring transit delays of packets across an Internet Protocol (IP) network. The history of the route is recorded as the round-trip times of the packets received from each successive host (remote node) in the route (path); the sum of the mean times in each hop is a measure of the total time spent to establish the connection. Traceroute proceeds unless all (three) sent packets are lost more than twice; then the connection is lost and the route cannot be evaluated. Ping, on the other hand, only computes the final round-trip times from the destination point.
 
BiGyElLoWhAt said:
I don't know much about batch

"Batch" meaning what, exactly? (I.e., what OS? Are we talking Windows .BAT, or now .CMD, files? Unix shell scripts?)

BiGyElLoWhAt said:
I would like to calculate the average, offset the average (shift it by (x - avg)) and then calculate the new average of everything > 0.

This will require significant parsing of the ping command output, which I don't think will be feasible in any batch language (Windows or Unix). I think you would be better off writing a script in a language like Python to parse the ping output.
 
One would normally spool the results into a file and then process it with something like awk to pull out selected fields from each line, sum them and return a value.

ping -n 200 | awk -e -f pingfilter.awk

pingfilter.awk
Code:
BEGIN {
    sum=0
}

/time=/ {
    split($7,parts,"=")
    sum+=parts[1]
    nsum++
}

END {
    print sum/nsum
}

or something like that
 
  • Like
Likes DavidSnider
Hmmm... Is there a way that I can execute a .bat file from these scripting languages. I'm looking to do most of the work up front to make it a 1 click check. I will probably use this on a daily or near daily basis. So I could probably do it in Java, create a bat file with the date and time that has the ping command and write that to a variable name. It would basically just be a string saved to a file saved as a bat file. The problem is then i have to delete these files everytime or my pc will be littered with bats and txt files containing the information.
 
jedishrfu said:
One would normally spool the results into a file and then process it with something like awk to pull out selected fields from each line, sum them and return a value.

ping -n 200 | awk -e -f pingfilter.awk

Does this file need to be in the same directory that I'm working in?
 
BiGyElLoWhAt said:
Is there a way that I can execute a .bat file from these scripting languages.

Why would you want to? You can do all the work in, for example, a Python script (including running the ping command and storing its output in a variable, you can do that from Python), and the Python script can be executed directly. Or you could call the Python script from a batch file if there are other associated programs you need to run before or after the Python script.
 
  • Like
Likes jedishrfu
Windows 10 now has a linux subsystem built into it. It's handy for this sort of thing.

As to the .awk file you could just pass it in as a string to awk if you wanted.

I don't really get the need to write a python script for something that coreutils can do pretty easily.
 
  • Like
Likes jedishrfu
You can find python examples online just search on “python ping”
 
  • #10
You might modify a Perl program like this to do what you need:
[CODE lang="perl" title="Perl calling ping and saving the times in an array"]
$pingDuration = 200;
$pings = `ping google.com -n $pingDuration`;

while( $pings =~ /time=(\d+)ms/g ){
push(@pingTimes, $1);
}

foreach $time (@pingTimes){
print "time = $time\n";
}
[/CODE]
I assume that you would have to download and install Perl, but you would have a very powerful and flexible tool once you have done that.
 
Last edited:
  • Like
Likes jedishrfu
  • #11
Here is a modification to the Perl program that will store the data in a single column .CSV ASCII file so you can import the data into EXCEL or other programs.
[CODE lang="perl" title="Perl program saving ping data to a .CSV file"]
$pingDuration = 200;
$pings = `ping google.com -n $pingDuration`;

while( $pings =~ /time=(\d+)ms/g ){
push(@pingTimes, $1);
}

open(OUT, ">pingTimeData.csv");
foreach $time (@pingTimes){
print OUT "$time\n";
}
close OUT;
[/CODE]
 
  • Like
Likes jedishrfu

Similar threads

Replies
11
Views
3K
Replies
4
Views
11K
Replies
1
Views
2K
Replies
12
Views
15K
Replies
1
Views
2K
Replies
7
Views
13K
Replies
13
Views
22K
Back
Top