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

BiGyElLoWhAt

Gold Member
1,560
113
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.
 

Klystron

Gold Member
401
426
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.
 
25,764
6,611
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?)

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.
 
10,686
4,243
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
 

BiGyElLoWhAt

Gold Member
1,560
113
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.
 

BiGyElLoWhAt

Gold Member
1,560
113
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?
 
25,764
6,611
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.
 

DavidSnider

Gold Member
454
107
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.
 
10,686
4,243
You can find python examples online just search on “python ping”
 

FactChecker

Science Advisor
Gold Member
2018 Award
4,979
1,748
You might modify a Perl program like this to do what you need:
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";
}
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:

FactChecker

Science Advisor
Gold Member
2018 Award
4,979
1,748
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.
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;
 

Want to reply to this thread?

"How to store ping results into an array or variable using a batch file" You must log in or register to reply here.

Related Threads for: How to store ping results into an array or variable using a batch file

  • Posted
Replies
2
Views
2K
Replies
2
Views
753
Replies
5
Views
647
Replies
4
Views
3K
  • Posted
Replies
11
Views
308

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top