编写批处理文件以检测ping异常

时间:2016-02-24 01:56:19

标签: batch-file cmd ping

我试图创建一个批处理文件来检测ping异常。我希望它无限地ping到IP(-t),直到我关闭它,只要ms>它就会写下来。 100毫秒和时间戳。我正在思考子字符串变量的某些方面,但我不知道如何绕过它。

2 个答案:

答案 0 :(得分:1)

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

或者这可能适合您的需求

ping /t > textfile.txt

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

答案 1 :(得分:0)

我一直在寻找同一个问题的答案。 bgalea的回答给了我自己写的东西。这就是我想出的:

:: 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

它适用于Windows 10.我没有在其他操作系统版本上测试过它。