Batch file to ping several hosts and log results

Since a connectivity issue between a Windows XP system in another country and systems in the United States seemed to correlate with the time of day that connectivity attempts were taking place, I wanted to have a batch file that would periodically ping from the source to the destination hosts and record the results, so that I could determine if packet loss was occurring at particular times every day because of contention with other traffic. So I created the following batch file (pinghosts.bat):

@echo off

REM pinghosts.bat
REM Version: 1.0
REM Created: 2017-05-17
REM Last modified: 2017-05-18
REM
REM Purpose: ping specified hosts logging ping results
REM URL: http://support.moonpoint.com/os/windows/commands/batch/pinghosts

set pingCount=5
set timeOut=500
set dirPath=%HOMEPATH%\Documents

REM ping google.com, apple.com, and cisco.com
for %%i in ("216.58.217.142" "17.142.160.59" "72.163.4.161") do (
   if not exist %dirPath%\%%i.txt (
      systeminfo | find "Time Zone:" > %dirPath%\%%i.txt
   )
   echo. >> %dirPath%\%%i.txt
   echo %date% %time% >> %dirPath%\%%i.txt
   ping -n %pingCount% -w %timeOut% %%i >> %dirPath%\%%i.txt
)

The pingCount variable determines how many ICMP echo request packets the ping utility will send and the timeOut variable tells ping how long it should wait for an echo reply packet before concluding that it won't receive the echo reply and displaying "Request timed out." The timeout value is specified in milliseconds. The default value is 4,000 milliseconds, i.e., 4 seconds. The dirPath variable determines which directory the output file will be placed in. In this case it will be the Documents directory beneath the user's home directory, which is usually \Users\acctname where acctname is the name of the account from which the command is issued. E.g., if the user account is jasmith1:

C:\>echo %HOMEPATH%
\Users\jasmith1

C:\>

To have the batch file ping several hosts sequentially, I can use the following putting the IP addresses or fully qualified domain names within parentheses:

for %%i in ("216.58.217.142" "17.142.160.59" "72.163.4.161") do (
   some commands
)

At the beginning of the output file that holds the results of the pings, I want to include a line that reveals the time zone for the system in the other country, since I don't know if the time zone is set for the local time or if the system is using Coordinated Universal Time (UTC) for the time zone. I only want to do that the first time the script is run, so I check to see if the file does not exist, which you can do with a command like the one below:

if not exist filename (
      some commands
   )

In this case, I search the output of the systeminfo command for the line that contains the time zone information and write it to the output file. The output file will be placed in the directory specified by the contents of the dirPath variable with a name based on %%i.txt, so in the example above as the for loop is executed the file names produced will be 216.58.217.142.txt, 17.142.160.59.txt, and 72.163.4.161.txt.

I next put a blank line in the file with echo. (putting a period immediately after the echo command will cause it to display a blank line).

Each time the batch file is executed, I want to display the current date and time which can be done by displaying the values for the environment variables %date% and %time%.

I then have the ping command send the specified number of ICMP echo request packets and wait the specified number of milliseconds.

I want the batch file to be executed every 10 minutes; I can use the Windows Task Scheduler schtasks command to achieve that goal:

C:\Users\jasmith1\Documents\bin>schtasks /create /tn PingTest /sc minute /mo 10 /tr "C:\Users\jasmith1\Documents\bin\pinghosts.bat"
SUCCESS: The scheduled task "PingTest" has successfully been created.

C:\Users\jasmith1\Documents\bin>

The /create option creates a new scheduled task and the /tn option gives the new batch job the name "PingTest". The sc minute option schedules the job to be run at an interval that will be in minutes with the number of minutes specified in the /mo modifier option. The /tr option specifies the location and name of the file that I want to be executed. For the interval, you can specify minute, hourly, daily, weekly, or monthly. For more details on the parameters you can use with the schtasks command - see Scheduling a task to run periodically with schtasks and Microsoft's XP Professional Product Documentation Schtasks page.

You can find when the scheduled job will next run by issuing a schtasks /query command and filtering the results with the find command.

C:\Users\jasmith1\Documents\bin>schtasks /query | find /i "pingtest"
PingTest                                 5/17/2017 10:38:00 PM  Ready

C:\Users\jasmith1\Documents\bin>

If you don't filter the results with the find command, you can see more detailed information on the task, but you will see information on other scheduled tasks as well:

Udemy Generic Category (English)120x600
C:\Users\jasmith1\Documents\bin>schtasks /query

Folder: \
TaskName                                 Next Run Time          Status
======================================== ====================== ===============
Adobe Acrobat Update Task                5/18/2017 2:00:00 PM   Ready
PingTest                                 5/17/2017 10:38:00 PM  Ready
Smart card mapping script                N/A                    Ready
User_Feed_Synchronization-{342E638C-896A 5/18/2017 6:53:05 PM   Ready

Folder: \Microsoft
TaskName                                 Next Run Time          Status
======================================== ====================== ===============
INFO: There are no scheduled tasks presently available at your access level.

Folder: \Microsoft\Office
TaskName                                 Next Run Time          Status
======================================== ====================== ===============
Office 15 Subscription Heartbeat         5/18/2017 12:07:37 AM  Ready
OfficeTelemetryAgentFallBack2016         N/A                    Ready
OfficeTelemetryAgentLogOn2016            N/A                    Ready

You can delete a scheduled task to stop it from running again using the /delete option to the schtasks command. E.g.:

C:\>schtasks /delete /tn "PingTest"
WARNING: Are you sure you want to remove the task "PingTest" (Y/N)? y
SUCCESS: The scheduled task "PingTest" was successfully deleted.

C:\>

Related articles:

  1. Batch file to display average ping times
  2. Scheduling a task to run periodically with schtasks

References:

  1. Ping
    Windows XP Professional Product Documentation
    Microsoft
  2. Schtasks
    Windows XP Professional Product Documentation
    Microsoft