10

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.

5 Answers 5

14
@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!

Sign up to request clarification or add additional context in comments.

4 Comments

Using pathping with -n -q 5 speeds things up and give detailed stats. Or tracert with -d option. These remove the dns resolve time (which is sort of pointless to do each time... it's always going to be the same) and gives a better chance of catching a transient failure upstream.
Surely the DNS is NOT always going to be the same each time. On one of my networks, the DNS server is usually the problem!
I want to do the same, but in lunux (ubuntu 18.04), any suggestions?
Thanks for the script, will see if it can catch the issues I'm having currently. I did modifiy it a bit: @echo off set Address=google.com set Delay=5 echo %date% %time% Ping %Address% every %Delay% seconds >> C:\pingtest\logfile.log :Loop PING -n %Delay% 127.0.0.1>nul echo %date% %time% Pinging %SystemRoot%\system32\ping.exe -n 1 %Address% | %SystemRoot%\system32\find.exe "TTL=" > NUL >> C:\pingtest\logfile.log if %ERRORLEVEL% EQU 0 goto :Loop echo %date% %time% Trace route %Address% >> C:\pingtest\logfile.log tracert %Address% >> C:\pingtest\logfile.log goto Loop
5
@echo off
set Address=www.google.com
set LogDir=C:\pingtest
md %LogDir%
%SystemRoot%\explorer.exe "%LogDir%"
echo PingTest script to monitor network connection.  Control-C to exit.
echo Tests connection by pinging %Address%.  Logs to %LogDir%\logfile.log.
echo %date% %time% Initial tracert (trace route) to %Address% >> %LogDir%\logfile.log
tracert %Address% >> %LogDir%\logfile.log
:Loop
REM  5 second delay
PING -n 5 -w 1 127.0.0.1>nul
echo %date% %time% Pinging %Address%
echo %date% %time% Pinging %Address% >> %LogDir%\logfile.log
%SystemRoot%\system32\ping.exe -n 1 %Address% | %SystemRoot%\system32\find.exe "TTL=" > NUL
if %ERRORLEVEL% EQU 0 goto :Loop
echo %date% %time% PING ERROR - Tracing route to %Address%
echo %date% %time% PING ERROR - Tracing route to %Address% >> %LogDir%\logfile.log
tracert %Address% >> %LogDir%\logfile.log
goto Loop

Comments

4

You could make a simple batch file that tries a ping and if it fails does a tracert, eg:

setlocal
set host=www.bigpond.com
set logfile=nettest.log
echo %date% %time%>>%logfile%
ping %host%>>%logfile%
if ERRORLEVEL 1 tracert %host%>>%logfile
endlocal

There's plenty of scope for refinement here.

Then create a scheduled task that runs it every five minutes or whatever suits you.

Alternatively you could include a loop with a 'sleep' in it. There's a poor man's sleep at Sleeping in a batch file that uses:

choice /d y /t 5 > nul

2 Comments

Ever since Windows 7 and Server 2018 we have the timeout command.
Thanks @MicheldeRuiter, I suspect I hadn't moved onto WIndows 7 when I wrote this. Thanks for the improvement.
2
:LOOP
FOR /F "usebackq tokens=1" %%F IN (`ping localhost -n 1 -w 1 ^| find "Request"`) DO (
  IF "%%F"=="Request" (
    tracert localhost
  )
)>>log.txt
FOR /F "usebackq tokens=1-4 delims=:." %%G IN (`echo %time%`) DO IF %G%H GTR 1400 GOTO:EOF
GOTO LOOP

Basically, this states do ping, if it finds a line that has an instance of the word Request (which only appears if you can't ping the address) perform a tracert. The -n and -w switches in PING tell it to jump only once and timeout after 1 second of not getting a response. This is perfectly fine if you are pinging your localhost. The second FOR statement is to have a stopping point. Change the 1400 to a time you wish for the script to stop (in military time of course).

1 Comment

This line didn't work for me: FOR /F "usebackq tokens=1-4 delims=:." %%G IN (echo %time%) DO IF %G%H GTR 1400 GOTO:EOF it had to be: FOR /F "usebackq tokens=1-4 delims=:." %%G IN (echo %time%) DO IF %%G%%H GTR 1400 GOTO:EOF
0

I have just been looking for the same thing to investigate why a VPN keeps dropping on a wired connection, used one of the batch file suggestions above which was great. Also found a nice little Java App which packages it for you here Internet Connectivity Monitor

Simple to use and does the job :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.