工作1循环代码:
for /l %i in (1,1,254) do @ping 131.212.30.%i -n 1 -w 100 | find "Reply"
在我尝试使用计数器时没有运行代码,所以每次ping得到回复时我们都会在网上添加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 "Reply" SET /A online=online+1
非常感谢。
答案 0 :(得分:1)
您的语法略有错误。以下内容应该有效(为了便于阅读,可以分成几行:
for /l %%i in (1,1,254) do (
for /l %%j in (1,1,254) do (
ping 131.212.%%i.%%j -n 1 -w 100 | find "Reply"
)
)
请记住,这是很多要ping的IP。
答案 1 :(得分:1)
Reply from 146.57.239.18: Destination host unreachable
目标无法访问,因此您的本地主机(146.57.239.18)回复"目标主机无法访问")。
146.57.239.18不是ping主机,而是您的localhost。
最好搜索TTL=
而不是Reply
:
...
ping 131.212.%%i.%%j -n 1 -w 100 | find "TTL="
...
您的set /a online=%online%+1
也不起作用。你需要delayed expansion。 set /a online +=1
语法效果更好:
...
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.
编辑更快的解决方案(并行工作):
@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