具有嵌套循环的批处理脚本,可对IP的ping范围进行嵌套 [英] Batch script with nested loops to ping range of IP's
问题描述
工作1个循环代码:
for /l %i in (1,1,254) do @ping 131.212.30.%i -n 1 -w 100 | find "Reply"
在我尝试使用计数器的地方没有运行代码,因此每次ping收到答复时,我们都会在网上添加1:
Not running code where I try to use a counter so every time the ping gets a reply we add 1 to online:
SET online=0 for /L %i in (1,1,254) do for /L %j in (1,1,255) do @ping 131.212.%i.%j -n 1 -w 100 | find "Reply" SET /A online=online+1
非常感谢.
推荐答案
Reply from 146.57.239.18: Destination host unreachable
目的地不可达,因此您的本地主机(146.57.239.18)答复目的地主机不可达".
The destination is not reachable, so your local host (146.57.239.18) replies with "Destination host unreachable").
146.57.239.18不是ping通的主机,而是您的本地主机.
146.57.239.18 is not the pinged host, but your localhost.
最好搜索TTL=
而不是Reply
:
...
ping 131.212.%%i.%%j -n 1 -w 100 | find "TTL="
...
您的set /a online=%online%+1
也不起作用.您需要延迟扩展. set /a online +=1
语法效果更好:
Also your set /a online=%online%+1
doesn't work. You would need delayed expansion. The set /a online +=1
syntax works better:
...
ping 131.212.%%i.%%j -n 1 -w 100 | find "TTL=" && SET /A online +=1 || set /a offline +=1
...
结果,整个代码如下:
SET online=0
for /L %%i in (1,1,254) do for /L %%j in (1,1,255) do ping 131.212.%%i.%%j -n 1 -w 100 | find "TTL=" && SET /A online +=1
echo %online% hosts are online.
编辑是一种更快的解决方案(并行工作):
EDIT a much quicker solution (working parallel):
@echo off
SET online=0
for /L %%i in (1,1,254) do (
start /min "pinging" cmd /c "(@for /L %%j in (1,1,255) do @ping 146.254.%%i.%%j -n 1 -w 100 | find "TTL=") >ping%%i.txt"
)
:loop
timeout /t 1 >nul
tasklist /v | find "pinging" && goto :loop
pause
for /f %%i in ('type ping*.txt 2^>nul^|find /c "TTL="') do echo %%i hosts are online
del ping*.txt
这篇关于具有嵌套循环的批处理脚本,可对IP的ping范围进行嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!