命令提示符编程批处理

时间:2016-03-20 14:13:30

标签: cmd

我想制作一个批处理文件来ping {IP地址1.30.200.3

Pinging 1.30.200.3 with 32 bytes of data:
Reply from 1.30.200.3: bytes=32 time=28ms TTL=124
Reply from 1.30.200.3: bytes=32 time=28ms TTL=124
Reply from 1.30.200.3: bytes=32 time=27ms TTL=124
Reply from 1.30.200.3: bytes=32 time=27ms TTL=124

Ping statistics for 1.30.200.3:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 27ms, Maximum = 28ms, Average = 27ms 

我需要询问"Received" >= 2是否只输入一行如下:

Reply from 1.30.200.3: bytes=32 time=28ms TTL=124

否则

输入已接收的数量和"Failure"

OutPut赞这个 如果收到的数据包大于等于

"来自1.30.200.3的回复:bytes = 32 time = 28ms TTL = 124"

或者如果不超过等于2 OUTPut赞这个

"收到= 0&失败"

3 个答案:

答案 0 :(得分:0)

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

另一个例子

wmic /append:"textfile.txt" path win32_pingstatus where "address='127.0.0.1' and responsetime >= 0 and statuscode is not null" get responsetime,timestamprecord,statuscode

寻求帮助 -

wmic /?
wmic path Win32_PingStatus /?
wmic path Win32_PingStatus get /?
wmic format /?
wmic append /?

另见https://msdn.microsoft.com/en-us/library/aa394350%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396各种常数的含义。

答案 1 :(得分:0)

call :check www.google.com
call :check www.notexist.xxx
goto :eof

:check
ping %1>ping.tmp
for /f %%i in ('type ping.tmp^|find /c "TTL="') do set count=%%i
for /f "tokens=*" %%i in ('type ping.tmp^|find "TTL="') do set output=%%i
if %count% geq 2 (echo %output%) else (echo Received = %count% ^& Failure)
del ping.tmp

TTL=Recieved更可靠(它独立于语言)(并且更容易解析)

答案 2 :(得分:0)

您已经有其他答案了。基本上,此代码解析ping命令的输出。无论英语Windows 7输出什么,它都是硬编码的。这应该被认为是脆弱的,因为它可能不适用于其他语言,也可能不适用于其他Windows版本。

SETLOCAL ENABLEDELAYEDEXPANSION
SET EXITCODE=0

SET HOSTNAME=localhost
SET TEMP_FILE=%TEMP%\pingcount_!RANDOM!_!RANDOM!.tmp

ping>"%TEMP_FILE%" %HOSTNAME%
SET EXITCODE=%ERRORLEVEL%
IF %EXITCODE% NEQ 0 (
    ECHO host %HOSTNAME% not found
    GOTO TheEnd
)

SET REP_LINE=

FOR /F "usebackq skip=1 tokens=*" %%a IN (`type "%TEMP_FILE%"`) DO (
    IF "!REP_LINE!" EQU "" (SET REP_LINE=%%a)
)

FOR /F "usebackq tokens=1-7 delims=, " %%a IN (`FINDSTR /C:"Packets: Sent" "%TEMP_FILE%"`) DO (
    SET RCOUNT=%%g
)

IF %RCOUNT% GEQ 2 (
    ECHO %REP_LINE%
) ELSE (
    ECHO Received = %RCOUNT%, Failure
)

:TheEnd

IF EXIST "%TEMP_FILE%" (DEL "%TEMP_FILE%"
EXIT %EXITCODE%
相关问题