Script to start traceroute if continuous ping fails, output to log

lysdexic picture lysdexic · Jul 26, 2011 · Viewed 47.6k times · Source

I want to continuously ping my home public IP address, and if the ping fails automatically do a traceroute to see where it's failing.

I've been trying to follow the comments made here:

http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/efc97c66-60a6-4fd7-8be4-4b454d040ce5

Windows compatible would be preferable, bat or vbs would be best.

From anywhere on the internet I will lose my connection to my home network. From work I have started a ping and when it drops I've done a traceroute and it fails before it gets to my IP.

I need a log file to prove that it is not my modem, or router, or computer.

Answer

lysdexic picture lysdexic · Jul 27, 2011
@echo off
set Address=google.com
:Loop
PING -n 5 127.0.0.1>nul
echo Pinging %Address%
%SystemRoot%\system32\ping.exe -n 1 %Address% | %SystemRoot%\system32\find.exe "TTL=" > NUL >> C:\pingtest\logfile.log
if %ERRORLEVEL% EQU 0 goto :Loop
echo Trace route %Address% at %date% %time% >> C:\pingtest\logfile.log
tracert %Address% >> C:\pingtest\logfile.log
goto Loop

This is what I ended up going with, if anyone else ever needs this. Essentially the "Ping -n 127.0.0.1>Nul" is to add a 5 second counter so that it only pinged the destination every 5 seconds, 5 can be changed to whatever value is needed.

Windows 7 has this problem where a ping may result with something like "reply from 192.168.1.5: Destination host unreachable". So instead of erroring out it gets a reply from itself and not the error level 1. Instead of looking for Error Level 1 I choose to look for no result for TTL with "%SystemRoot%\system32\ping.exe -n 1 %Address% | %SystemRoot%\system32\find.exe "TTL=" > NUL"

Anyway, I'm sure the other answers here were very similar and may have worked, so I am ranking them up, but marking this as the answer.

Thanks all!