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

In summary: Net::Ping;my $pingDuration = 200;my $pings = Net::Ping->new(Hostname => 'google.com',Port => 80,Timeout => $pingDuration,Timeout_Seconds => 5);while( $pings->get() ){$time = $ping->packet_time;push(@pingTimes, $time);}foreach $time (@pingTimes){print "time = $time\n";}The Perl code is straightforward. It
  • #1
BiGyElLoWhAt
Gold Member
1,622
131
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
  • #2
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.
 
  • #3
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.
 
  • #4
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
  • #5
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.
 
  • #6
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?
 
  • #7
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
  • #8
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
  • #9
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:
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:
  • 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.
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;
 
  • Like
Likes jedishrfu

1. How can I store ping results into an array or variable using a batch file?

To store ping results into an array or variable using a batch file, you can use the "FOR /F" command, which allows you to loop through the output of a command and assign it to a variable. For example, you can use the command "FOR /F "tokens=*" %%a IN ('ping [IP address]') DO SET ping_results=%%a" to store the output of the ping command into the variable "ping_results".

2. Can I store multiple ping results into a single array or variable?

Yes, you can store multiple ping results into a single array or variable using the same "FOR /F" command. You can use the "tokens" option to specify which part of the output you want to store, and then use the "%%a" variable to access each result in the loop. For example, you can use the command "FOR /F "tokens=3 delims= " %%a IN ('ping [IP addresses]') DO SET ping_results=%%a" to store the third ping result from each IP address into the variable "ping_results".

3. Is it possible to store ping results into an array or variable from a text file?

Yes, it is possible to store ping results into an array or variable from a text file using the "FOR /F" command. Instead of using the ping command as the input, you can specify a text file using the "IN" option. For example, you can use the command "FOR /F "tokens=*" %%a IN (ping_results.txt) DO SET ping_results=%%a" to store the contents of the "ping_results.txt" file into the variable "ping_results".

4. How can I access the stored ping results in the array or variable?

To access the stored ping results in the array or variable, you can use the "%%a" variable in a loop. For example, you can use the command "FOR /F "tokens=*" %%a IN ('ping [IP address]') DO ECHO %%a" to display each result in the loop. You can also use the "ping_results" variable directly in your batch file commands.

5. Can I save the stored ping results into a text file?

Yes, you can save the stored ping results into a text file using the ">>" operator. You can use the command "FOR /F "tokens=*" %%a IN ('ping [IP address]') DO ECHO %%a >> ping_results.txt" to append the ping results from each IP address to the "ping_results.txt" file. If you want to overwrite the file instead, you can use a single ">" instead of ">>".

Similar threads

  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
4
Views
11K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
12
Views
14K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
7
Views
12K
Replies
2
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
13
Views
21K
  • Computing and Technology
Replies
1
Views
2K
Back
Top