2

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:

  • Dial the connection via OpenVPN (which I have working fine)
  • Ping a host on the other side of the VPN and don't continue through the batch file until this host is reachable.

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.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Kane Charles
  • 379
  • 3
  • 10
  • 19

3 Answers3

9

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

Community
  • 1
  • 1
spooky
  • 1,620
  • 1
  • 13
  • 15
4

as already mentioned years ago, output of ping is language dependent, so it's not a good idea to rely on a string like Received. The preferred and most reliable method is searching for the string TTL=:

:loop
timeout 2
ping -n 1 %ipaddress% |find "TTL=" || goto :loop
echo Answer received.

|| works as "if previous command (find) wasn't successful, then"

(as for the timeout: never build a loop without some idle time to reduce CPU load)

Stephan
  • 53,940
  • 10
  • 58
  • 91
0

It works if You translate the "Received" string using the string that your language uses for the word Received (i.e. in Italian is Ricevuti).