0

Working 1 loop code:

for /l %i in (1,1,254) do @ping 131.212.30.%i -n 1 -w 100 | find "Reply"

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

Thanks a lot.

Mike
  • 19
  • 2
  • 7
  • 1
    Is this kind of what you're wanting to do? [Ping range of IP](http://stackoverflow.com/questions/32572939/ping-a-user-defined-ip-range-with-batch) – Potato Head Sep 28 '15 at 00:39
  • @bliziforst Sort of, but that doesn't really help me understand the nested loops and why they end up pinging a random IP. Thanks for your time! – Mike Sep 28 '15 at 00:48

2 Answers2

1

You have the syntax slightly wrong. The following should work (split over a couple of lines for readability:

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"
  )
)

Keep in mind that this is a lot of IPs to ping.

clstrfsck
  • 14,715
  • 4
  • 44
  • 59
1
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
Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91