-1

I'm trying to create a batch file where it would detect ping anomalies. I want it to ping to an IP infinitely (-t) until I close it where it would write down whenever ms > 100 ms and the time stamp as well. I'm thinking somewhere along the lines of sub string variables but I don't know how to wrap my head around it.

Antonio
  • 11
  • 2

2 Answers2

1
:Loop
time /t >> textfile.txt
ping -n 1 127.0.0.1 | findstr /c:"Minimum" >> textfile.txt
timeout /t 5
Goto Loop

Or perhaps this suits your needs

ping /t > textfile.txt

or

:loop
    wmic /append:"textfile.txt" path win32_pingstatus where "address='127.0.0.1' and responsetime > 100" get responsetime,timestamprecord
goto loop
0

I've been looking for an answer to this same question for while. bgalea's answer gave me the pieces I needed to write my own. Here's what I came up with:

:: usage: badpings.bat [ip adress | hostname] [ping time threshhold]

@echo off
if "%1"=="" (
    set pingdest=yahoo.com
    ) else (
    set pingdest=%1
    )

if "%2"=="" (
    set /a limit=100
    ) else (
    set /a limit=%2
    )
echo Pinging %pingdest%.
echo Logging replies over %limit%ms.
echo Press Ctrl+C to end.

:Loop
for /f "usebackq tokens=1-6" %%a in (`ping -n 1 %pingdest% ^| findstr "Request Reply request"`) do (
    set var=%%a %%b %%c %%d %%e %%f
    set pingtimestr=%%e
    )

if "%pingtimestr%"=="find" (
    echo Ping request could not find host %pingdest%. Please check the name and try again.
    goto End
    ) 
if "%pingtimestr%"=="host" (
    set /a pingtime=%limit%+1   
    ) 
if "%pingtimestr:~0,4%"=="time" (
    set /a pingtime=%pingtimestr:~5,-2% 
    )
if %pingtime% GTR %limit% (
    echo [%time%] %var%>>badpings.log
    echo [%time%] %var%)
timeout /t 1 /nobreak >nul
Goto Loop
:End

It works on Windows 10. I haven't tested it on other OS versions.

Wes Larson
  • 1,042
  • 8
  • 17