CMD if ping successful do this

12,674

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
)
Share:
12,674

Related videos on Youtube

Reuter00
Author by

Reuter00

Updated on June 04, 2022

Comments

  • Reuter00
    Reuter00 almost 2 years

    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.

    • MC ND
      MC ND about 6 years
      Probably this and this could explain the observed behaviour