Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 批处理脚本ping多个ip_Batch File_Ping - Fatal编程技术网

Batch file 批处理脚本ping多个ip

Batch file 批处理脚本ping多个ip,batch-file,ping,Batch File,Ping,我想制作一个脚本来ping多个IP地址。但我不想使用errorlevel 我想对每个IP地址ping 5次,并显示结果: 如果5个数据包丢失,我想显示回显%ip-DOWN 如果3个数据包丢失,我想显示一个回显%ip-3丢失 等等 所以我想从这一行的变量中取回“丢失的数据包”: “数据包:发送=4,接收=4,丢失=0(0%丢失) 这是我的代码,但不起作用 echo off set perte=0 set servers=servers.txt for /f %%i in (%serv

我想制作一个脚本来ping多个IP地址。但我不想使用errorlevel

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

  • 如果5个数据包丢失,我想显示回显%ip-DOWN
  • 如果3个数据包丢失,我想显示一个回显%ip-3丢失
  • 等等
所以我想从这一行的变量中取回“丢失的数据包”:

“数据包:发送=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

令牌、delims和find命令已更改

@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
差不多了,试试看

@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

call::testping%%i
-为什么这里有双冒号?如果使用“tokens=3”“,使用G作为for循环变量,可以得到%%G%%H和%%I,因此%%p?call::testping%%I-为什么这里有双冒号?–Npcmaka 4分钟前-->因为是为BatchFor MC调用函数的规则。谢谢,我有测试,但不起作用。我仍然使用双冒号,但它不是正常的语法。您将
%%G
更改为
%%a
,因此您现在得到了
%%a
%%b
%%c
,但再次没有
%%p
,也非常感谢您。当我写文章时,我明白了我的错误(我的代码现在很好)