Reply from 146.57.239.18: Destination host unreachable
目的地不可達,所以當地的主機(146.57.239.18)回覆「目標主機不可達」)。
146.57.239.18不是ping主機,而是你的本地主機。
這是更好地尋找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
是這種你想做什麼? [平的IP範圍](http://stackoverflow.com/questions/32572939/ping-a-user-defined-ip-range-with-batch) –
@bliziforst有點,但這並沒有真正幫助我理解嵌套循環以及爲什麼他們最終會ping一個隨機IP。謝謝你的時間! – Mike