Determine Network availability with PING and run batch file when Ping Times out

Hello Experts!

I am having a problem where an open source VPN router intermittently goes down (IPSEC VPN Drops..)

When this happens, I have to manually restart the VPN service to bring the VPN tunnel back up...

Is it possible to use a batch file to use PING (just sending 1byte packets for low traffic)

When receiving ping replies... everything is good...

But when the pings timeout that means the VPN tunnel is down.

Can you provide an example batch file that will ping a remote host, then if there are more than, let's say for example 10 timeouts.. then the script will recognize this and execute a command to clear the tunnel?

I can provide the rest, i'm just looking for the logic/script code to determine if the network is timing out and if so, run a command.

Is this possible with Batch? Does anyone have any other suggestions?

(BTW, network monitoring tools aren't really an option as I haven't found one that can automate this particular VPN process. I've found ones which will send me an alert/email, but I'm looking for something that is capable of running a command (or calling on another batch script) to fix the tunnel.

Thanks experts!
LVL 1
zequestionerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Something like that? Add your recovery code starting at line 19.
@echo off
setlocal
REM *** Host to test
set PingHost=127.0.0.1
REM *** Number of pings to send with each test:
set PingCount=10
REM *** Size of the ping packets in byte:
set PingSize=8
REM *** Time in seconds to wait between checks:
set Pause=60
:PingLoop
ping -4 -l %PingSize% -n %PingCount% | find "TTL" >NUL
if not errorlevel 1 (
	ping.exe localhost -n %Pause% >NUL
	goto PingLoop
)
color 4F
echo VPN tunnel lost, trying to re-establish ...
REM ...
color
echo Continuing check ...
goto PingLoop

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zequestionerAuthor Commented:
This is sweet. It doesn't quite work as posted (no host defined on the ping command) but it gets the job done.

Made small edits to make it look a bit prettier as it goes thru the checks...

I tested it by running the script, then disabling my network card.

wmic path win32_networkadapter where index=7 call disable

Then, line 37 of the script re-enables it.. I'll change this line to call my other script which resets the vpn.

Thanks again for your help.

Here's my final version:

@echo off
setlocal
REM *** Host to test
set PingHost=TestServer.com
REM *** Number of pings to send with each test:
set PingCount=10
REM *** Size of the ping packets in byte:
set PingSize=8
REM *** Time in seconds to wait between checks:
set Pause=60
:PingLoop
cls
color 89
echo .
echo . VPN Connection Active
echo . 
echo . Monitoring Connectivity ...
echo . 

ping %PingHost% -4 -l %PingSize% -n %PingCount% | find "TTL" >NUL

if not errorlevel 1 (
	ping.exe %PingHost% -n %Pause% >NUL
	goto PingLoop
) else (
	goto vpn_down
)

:vpn_down

color 4F
echo .
echo . VPN tunnel lost, trying to re-establish ...
echo . 

REM // Replace the following line with a call to the VPN script
wmic path win32_networkadapter where index=7 call enable

REM // Wait for the network to come back up 
ping -n 15 localhost > nul

REM // Check if network is back online
ping %PingHost% -4 -l %PingSize% -n %PingCount% | find "TTL" >NUL

if not errorlevel 1 (
	ping.exe %PingHost% -n %Pause% >NUL
	goto PingLoop
) else (
	goto vpn_down
)

goto PingLoop

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.