loopsbatch-filepinghostopenvpn

Pause a batch file until a host is reachable (using ping)?


I'm looking to add some functionality to a batch file that I've been writing; essentially what happens is I dial up a VPN connection using openvpn and then continue to mount a network drive as well as many other things, what I'm looking to do is:

Currently I've been using a sleep command of 20 seconds which works, but is not a very clean or intelligent way of going about it; I'd imagine I need some sort of loop to attempt to ping the host infinitely until it is reachable before continuing in the batch file. Any help would be greatly appreciated.


Solution

  • from antoher thread on stackoverflow... credit to paxdiablo (find the original post here)

    @setlocal enableextensions enabledelayedexpansion
    @echo off
    set ipaddr=%1
    :loop
    set state=down
    for /f "tokens=5,7" %%a in ('ping -n 1 !ipaddr!') do (
        if "x%%a"=="xReceived" if "x%%b"=="x1," set state=up
    )
    echo.Link is !state!
    ping -n 6 127.0.0.1 >nul: 2>nul:
    goto :loop
    endlocal
    

    This will give you enough ammo to use and solve your problem