Reply from 146.57.239.18: Destination host unreachable
The destination is not reachable, so your local host (146.57.239.18) replies with "Destination host unreachable").
146.57.239.18 is not the pinged host, but your localhost.
It's better to search for TTL=
instead of Reply
:
...
ping 131.212.%%i.%%j -n 1 -w 100 | find "TTL="
...
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
...
As a result, the whole code would look like:
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