So I’m trying to troubleshoot a problem where backups of a Windows 2008 server over a 1.5Mbps Wide Area Network (WAN) connection keep failing. Occasionally it will work, but most nights it will fail after running for several hours. We have a number of in-house network troubleshooting tools and I also know there are endless ping utilities that could assist. But instead of fancy utilities, I want a simple batch file to do the job. I want to know when this remote Windows server is unable to ping the destination network.
Create a simple batch file that outputs to a text file ONLY when a ping fails and also include the time of the failure.
If you run this ping command:
ping -n 1 10.1.1.1
The result will be this (assuming 10.1.1.1 is inaccessible):
Pinging 10.1.1.1 with 32 bytes of data:
Request timed out.
Ping statistics for 10.1.1.1:
Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
So what I want to do is look for the “Lost =1” and when I find it, I want to append it to a text file along with a time stamp.
After concatenating many Google searches along with some trial and error, I’ve come up with a batch file that will ping a particular IP address once per second. If any individual ping results in a “Request timed out” the batch file will specifically look for “Lost = 1” in the ping results, and when found, will append the following string (with current date and time) to a new line in a text file:
Mon 10/21/2013 12:31:09.91 - [ Lost = 1 (100% loss)]
So every instance of the above line is a ping failure and all successful pings will NOT be recorded to the file. This allows me to open a small text file and at a glance see the specific times a particular IP address was inaccessible. The hope is to be able to line up ping failures with backup failures. But this could be used in troubleshooting many random timeout errors in applications, etc.
Below is the complete batch file (let’s call it pingloss.cmd) that will append failed pings to a text file along with a date and time stamp:
for /f "tokens=*" %%A in ('ping -n 1 %1 ^|find "Pinging %1"') Do echo %%A >>pingloss.txt
FOR /f "tokens=3 delims=," %%A IN ('ping -n 1 %1 ^|find "Lost = 1"') DO echo %date% %time% - [%%A] >>pingloss.txt
choice /c x /t 1 /d x >nul
– The for /f command scans the output of the ping command looking for specific area of text by delimiter (in this case a comma).
– The find command isolates the DO action of the for command to only act on occurrences of “Lost = 1”.
– The choice command is simply used to create a (1) second pause between pings.
– The purpose of the first line before the :START section is to append a single line to the text file that tells you which IP address it is pinging. This line denotes the beginning of a new test.
To use the batch file, simply run it like this (where x.x.x.x is the IP address you want to ping):
As soon as you run the file, the following line will be appended to a text file (in the same directory) called pingloss.txt:
Pinging x.x.x.x with 32 bytes of data:
No further lines will appear unless their is a ping failure.
So you can simply kick this batch file off in a command window and let it run while you do your testing. In this case, I’ll be kicking off the batch file and waiting for the nightly scheduled backup to occur. Next time this backup fails, I’ll check the pingloss.txt file to see if there is a ping failure that corresponds with the backup failure. At least then I can confirm (well, mostly) a network error and not a server error.