MoonPoint Support Logo

 

Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals



Advanced Search
June
Sun Mon Tue Wed Thu Fri Sat
 
15
       
2015
Months
Jun


Mon, Jun 15, 2015 10:23 pm

Batch file to display average ping times

If you run the ping command in windows with no parameters specified other than the system to ping, the output will look similar to the following:

C:\>ping google.com

Pinging google.com [216.58.217.142] with 32 bytes of data:
Reply from 216.58.217.142: bytes=32 time=12ms TTL=55
Reply from 216.58.217.142: bytes=32 time=16ms TTL=55
Reply from 216.58.217.142: bytes=32 time=13ms TTL=55
Reply from 216.58.217.142: bytes=32 time=13ms TTL=55

Ping statistics for 216.58.217.142:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 12ms, Maximum = 16ms, Average = 13ms

If I want to just see the ping times and no other output from the ping command, I could use a FOR /F command such as is shown below. The "pipe" character, |, used to pipe output from one command to another should be prefixed with ^, which serves as an "escape character".

C:\>for /f "tokens=7 delims== " %g in ('ping google.com ^| find "Reply from"') do @echo %g
22ms
15ms
13ms
22ms

To do the same thing from a batch file, I could put the following commands in a batch file:

@echo off

set HostToPing="google.com"

for /f "tokens=7 delims== " %%g in ('ping %HostToPing% ^| find "Reply from"') do echo %%g

The differences are that I would need to use %%g, instead of %g in the batch file and I could use echo in the batch file rather than @echo, since I put @echo as the first line of the batch file to turn off the display of the comands themselves.

By default, when you run the ping command on a Microsoft Windows system, it will issue 4 pings then stop. But you can control the number of pings sent by using the -n count parameter, where count is the number of pings to send.

    -n count       Number of echo requests to send.

If I want to monitor the average response time to pings to a given host over a long period of time, I could issue the ping command with a very large number of echo requests specified or use the -t parameter, e.g., ping -t to have the pings run indefinitely:

    -t             Ping the specified host until stopped.
                   To see statistics and continue - type Control-Break;
                   To stop - type Control-C.

I could store the results in a file by redirecting output from the command with the output redirection character >, i.e., ping -n 500000 google.com >pingoutput.txt. Or I could just look at the line where ping displays the average and create a loop where pings are run periodically. The line where the average is shown by ping looks like the following:

Minimum = 14ms, Maximum = 17ms, Average = 15ms

To just look at that average value, but also time stamp the output, so I can determine if ping times to a particular host are changing significantly over the course of a day, I could use code similar to the following in a batch file, substituting the particular host I'm interested in for google.com and using the timeout command present in Microsoft Windows 7/2008 and later versions to specify the pause period between instances of the ping command being run. The timeout command is similar to the sleep or wait commands that may be present in other operating systems. In the example below the timeout value is 2 minutes. In this case >> must be used to append to the output file, since > would overwrite the output file each time ping was run.

@echo off

set HostToPing="google.com"

:loop
set datetime=%date% %time%
for /f "tokens=9" %%g in ('ping %HostToPing% ^| find "Average ="') do echo %datetime% %%g >>ping_times.txt
timeout /t 120
goto loop

If I name the batch file avgpingtime.bat and run it from a command prompt, I would see the following until the batch file is stopped with Ctrl-C.

C:\>avgpingtime

Waiting for   0 seconds, press a key to continue ...

Waiting for   0 seconds, press a key to continue ...

Waiting for   0 seconds, press a key to continue ...

Waiting for   0 seconds, press a key to continue ...

Waiting for   0 seconds, press a key to continue ...

Waiting for   0 seconds, press a key to continue ...

Waiting for   0 seconds, press a key to continue ...

Waiting for   0 seconds, press a key to continue ...

Waiting for  14 seconds, press a key to continue ...

Until the batch file s terminated with Ctrl-C, it would display its countdown from 120 to 0 each time the timeout command is executed. The results stored in the text file would be similar to the following:

C:\>more google_ping_times.txt
Mon 06/15/2015 20:35:10.22 25ms
Mon 06/15/2015 20:37:13.15 33ms
Mon 06/15/2015 20:39:16.13 16ms
Mon 06/15/2015 20:41:19.12 22ms
Mon 06/15/2015 20:43:22.10 25ms
Mon 06/15/2015 20:45:25.13 22ms
Mon 06/15/2015 20:47:28.12 17ms
Mon 06/15/2015 20:49:31.12 20ms
Mon 06/15/2015 20:51:34.13 15ms
Mon 06/15/2015 20:53:23.37 20ms

The batch file could also be scheduled to run with the Windows Task Scheduler.

[/os/windows/commands] permanent link

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo