Using a BAT file to automatically reconnect a VPN Cisco on failure of connection

Rodolpho Pereira picture Rodolpho Pereira · Feb 28, 2014 · Viewed 15k times · Source

I would like help to create an automatic BAT script that reconnects my Cisco VPN Client Session in case of outage of Internet.

There are two command lines: command 1 to connect and command 2 to disconnect.

To monitor the connection I would like use ICMP (Like 5 failed pings) and if working VPN then continue to forward data. If not use command 2 to disconnect the VPN Session and try to reconnect again.

If possible I would like it to loop so it will auto-reconnect every time the WAN Link breaks.

command 1: The Client program is at: C:\Program Files (x86)\Cisco Systems\VPN

start vpnclient connect rcx user TESTE pwd TESTE stdin

command 2: The Client program is at: C:\Program Files (x86)\Cisco Systems\VPN

vpnclient disconnect

I made many attempts with no success.

Answer

foxidrive picture foxidrive · Mar 1, 2014

This should ping the IP address and on failure it will disconnect, pause for 10 seconds, start the vpnclient and pause for 10 seconds, then loop.

There is a 30 second delay between tests.

The timeout command is in Vista and later.

@echo off
cd /d "C:\Program Files (x86)\Cisco Systems\VPN"
:loop
    echo testing IP address
    ping 123.456.78.90 >nul || (
       echo ping failure - disconnecting
       vpnclient disconnect
          timeout /t 10 /nobreak
       echo reconnecting
       start vpnclient connect rcx user TESTE pwd TESTE stdin
          timeout /t 10 /nobreak
   )
timeout /t 30 /nobreak
goto :loop