Windows VPN Keep Alive

Batch file properties window.
Batch Shortcut

I enjoy the one-click facility for connecting to my VPN in Windows XP.  It gets the job done, but I sometimes struggle with the famous dead connection bug.  This is a very common problem in Windows that causes the VPN to become unresponsive after two to five minutes of inactivity, even though the status still says “Connected.”

I created a one-click solution for both connecting and maintaining a VPN.  Setting it up is simple.  It involves just these steps, which I will explain below:

  1. Set the VPN “idle time before hanging up” period to “5 minutes” instead of “never.”  This forces Windows to properly reflect any disconnection.
  2. Create a new batch file, which I have provided below.
  3. Edit the batch file to match the name and address of your connection.
  4. Create a desktop shortcut to the batch file.
  5. Edit the shortcut properties so that the batch automatically runs minimized with a nice icon.

My Intranet.bat

@echo off

rem Check if already connected
ping -n 1 -w 500 > nul && goto Notice

rasdial "My Intranet" || goto Failed
echo %date% %time%

echo Leave this window open for keep-alive service.

ping -n 30 > nul
ping -n 1 -w 500 && goto Loop
ping -n 1 -w 500 && goto Loop
echo Connection died %date% %time%
rasdial "My Intranet" /disconnect
goto Attempt

echo Connection failed %date% %time%


In the batch file above, there are two things that must be edited.  The phrase “My Intranet” shows up in two places, and it must be edited to match the name of your VPN connection.  Additionally, the IP address appears three times and must be edited to match the VPN segment gateway address.  This address can be found in the Details tab of the VPN status window when connected.  It is labeled “Server IP address.”

The desktop shortcut is created simply by holding down the Alt key while dragging the batch file to the desktop.  The default name is displayed as “Shortcut to My Intranet.bat” and it can be renamed safely to “My Intranet”.  The shortcut name does not have to match the connection name.  Right-click the shortcut to see its properties.  That is where you may select a nice icon and change the Run property from “Normal window” to “Minimized.”

To manually disconnect the VPN, first close the running batch window, then click Disconnect on the VPN icon.

Note this solution does not solve the problem of the Offline Files system disconnecting frequently from remote servers.  I will follow up with a new article when I figure out how to make that part behave better.

Getting Fancy

If you wish to further customize the batch file, take note of the following parameters:  “30” is the keep-alive interval in seconds.  “500” is the ping timeout in milliseconds.  The latter is used twice to allow for a single packet to be lost before assuming the link has died.

Last year, I wrote about how to easily create a split tunnel VPN to speed up non-intranet traffic while staying connected to your servers.  What I didn’t elaborate on in that article is that sometimes I find myself at a non-encrypted hot spot wanting to log in to a non-encrypted website.  In that situation, I want to take full advantage of my VPN encryption so that my traffic isn’t being broadcast in the clear, no matter how slowly it runs.  To give myself that flexibility, I simply keep two different VPN icons in the network connections panel.  One of them is a split tunnel connection with customized TCP/IP settings, and the other uses the default settings and the same server.  I’ve created two separate batch files now so that I can click on one icon or the other to get the desired connection and an automatic keep-alive signal.  When I’m wired, I click on my split tunnel.  When I’m wireless, I click on my remote gateway.  Problems solved! 🙂

3 thoughts on “Windows VPN Keep Alive”

Leave a Reply

Your email address will not be published.