Writing a batch file to detect ping anomalies

前端 未结 2 774
借酒劲吻你
借酒劲吻你 2021-01-29 08:59

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

相关标签:
2条回答
  • 2021-01-29 09:18
    :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 讨论(0)
  • 2021-01-29 09:28

    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.

    0 讨论(0)
提交回复
热议问题