3

i have situation that i can not understand, i am trying to do a batch file where he loops 200 times and in each loop he pings a host, if the ping is successful he does a command. Here is what i got:

    @echo off
for /L %%N IN (1, 1, 200) DO (
    ping -n 1 192.1.22.%%N
    if not ERRORLEVEL 1 (
        set pingresult=true
        goto done
    )
)

set pingresult=false
:done
if %pingresult% == true (
 echo Pikachu


) else (
  echo "Offline!"
)

it doesn't work.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Reuter00
  • 83
  • 1
  • 1
  • 7
  • 1
    Probably [this](https://stackoverflow.com/a/21252613/2861476) and [this](https://stackoverflow.com/a/27748080/2861476) could explain the observed behaviour – MC ND Apr 04 '18 at 14:08

1 Answers1

5

Thanks to aschipfl I discovered the answer!

you just have to use | find "TTL=" >nul after the ping command because this way if the ping was successful he devolve a TTL and the | find "TTL=" >nul will grep it for validation. This is the example from the website I got the solution.

ping -n 1 192.168.1.1 | find "TTL=" >nul
if errorlevel 1 (
    echo host not reachable
) else (
    echo host reachable
)
phuclv
  • 37,963
  • 15
  • 156
  • 475
Reuter00
  • 83
  • 1
  • 1
  • 7