2016-03-20 89 views
-2

我想編寫一個批處理文件來ping像1.30.200.3一個IP地址:命令提示符編程一批

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"

輸出像這樣 如果接收到的比且等於2包

「從1.30.200.3答覆:字節= 32時間= 28ms TTL = 124」

或者,如果不大於等於和2 輸出像此

「接收= 0 &失敗」

+0

解析'平1.30.200.3 |找到/ I 「的數據包:發送」 使用'命令['FOR/F'環](http://ss64.com/nt/for_cmd.html)。 – JosefZ

回答

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各種常量的意思。

+0

我需要這個命令提示代碼 –

+0

這是命令提示符。 –

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更可靠(它是獨立於語言的)(和更容易解析)

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%