0

我想制作一个脚本来 ping 多个 IP 地址。但我不想使用错误级别。

我想 ping 每个 IP 地址 5 次,并显示结果:

  • 如果 5 个数据包丢失,我想显示一个 echo %ip - DOWN
  • 如果丢失 3 个数据包,我想显示 echo %ip - 3 lost
  • ETC...

所以我想从这一行的变量中取回“丢失的数据包”:

“数据包:发送 = 4,接收 = 4,丢失 = 0(丢失 0%)

这是我的代码,但这不起作用。

echo off 

set perte=0 
set servers=servers.txt 

for /f %%i in (%servers%) do call :testping %%i 
goto:eof

:testping 
FOR /f "tokens=3 delims==" %%a IN (' ping -n 5 %1 ^|find "lost"') 
DO (set perte=%%b) 

if %perte%==1 echo %1 lost 1 ping
if %perte%==2 echo %1 lost 2 ping
if %perte%==3 echo %1 lost 3 ping
if %perte%==4 echo %1 lost 4 ping
if %perte%==5 echo %1 DOWN
if %perte%==0 echo %1 UP
goto:eof
4

2 回答 2

1

标记、分隔符和查找命令已更改。

@echo off 

set perte=0 
set servers=servers.txt 

for /f %%i in (%servers%) do call :testping %%i 
goto:eof

:testping 
FOR /f "tokens=7 delims== " %%a IN (' ping -n 5 %1 ^|find /i "lost"') DO (set perte=%%a) 

if %perte%==1 echo %1 lost 1 ping
if %perte%==2 echo %1 lost 2 ping
if %perte%==3 echo %1 lost 3 ping
if %perte%==4 echo %1 lost 4 ping
if %perte%==5 echo %1 DOWN
if %perte%==0 echo %1 UP
goto:eof
于 2013-10-28T04:02:53.843 回答
0

快完成了。尝试

@echo off 

    setlocal enableextensions 

    set servers=servers.txt

    for /f %%i in (%servers%) do call :testping %%i

    endlocal
    exit /b

:testping 
    rem
    rem How/Why delims are defined as =,(
    rem Packets: Sent = 5, Received = 5, Lost = 0 (0% loss), 
    rem ..............^..^..........^..^......^...^.........
    rem token: 1       2  3          4  5      6   7
    rem
    FOR /f "tokens=6 delims==,(" %%a IN (' ping -n 5 %1 ^| find /i "lost" ') do (
        if %%a EQU 0 (
            echo %1 UP
        ) else if %%a EQU 5 (
            echo %1 DOWN
        ) else (
            echo %1 lost %%a ping
        )
    )
    goto :EOF
于 2013-10-28T08:59:29.813 回答