Friday 20 December 2013

Windows - Automatic VPN dialer Batch script

Sometimes I need to work on several customers or environments at the same time. This means that sometimes I need to have several (Windows) VPN connections open at the same time or I need to connect and disconnect from them several times a day.

In order to simplify the process and to enable me to keep my network settings mostly undisturbed when I connect to VPNs I have created a simple batch file to allow me to quickly connect to a VPN and set the appropriate routes.

The batch file is available here: vpn_dialer_template.bat

Usually when you connect to a VPN you lose your default gateway and become unable to access your local resources. This can be avoided by configuring the connection not to use the VPN gateway, but this comes with a cost: you have to manually set the required routes yourself.

In order to make this process simpler and quicker I came up with a batch file that makes the VPN connection and set the routes all by itself. However, It has some "requirements": I only tested it on Windows 7, although it should work on Vista and Windows 8; it has to be run as Administrator since setting the routes requires privilege escalation; to make it effective you have to disable the "use default gateway" option on the VPN connection this implies you have to know the remote gateway address and other routes that you also need.

I'll guide you through the process of deactivating the remote gateway option:

First goto "Network and Sharing Center". Next, choose the "Change Adapter Settings" option on the left. Then right-click on the VPN connection and choose "Properties"




Then select the Networking tab.

In the Networking tab, select "Internet Protocol Version 4" and then click on Properties:


Click Advanced:


Uncheck the option: "Use default gateway on remote network" and click OK->OK->OK.


Now the VPN Connection is ready to use with the batch file.
Of course, you can also use this trick on your own.


Next I'll explain how the batch file is used.


You can configure your setting in the USER VARIABLES section in the beginning of the file:
REM **************************************
REM USER VARIABLES
REM **************************************

SET VPNNAME=VPNNAME
SET VPNUSER=vpnuser
SET VPNPASS=vpnpass
REM If you do not use a Domain then set VPNDOMAIN to empty: SET VPNDOMAIN=
SET VPNDOMAIN=vpndomain

REM ROUTES TO ADD AFTER CONNECT
REM EXAMPLE: ROUTE1=route add 10.110.0.0 mask 255.255.0.0 10.255.255.2
SET ROUTE1=
SET ROUTE2=
SET ROUTE3=
SET ROUTE4=
SET ROUTE5=

REM **************************************

You need to change the VPNNAME to the name you gave your VPN.
The VPNUSER, VPNPASS are the username and password used to connect to the VPN. If you have an issue with having the plain-text password in a file then you should placed it in an encrypted folder (that works for me).

The VPNDOMAIN is optional and you only need to set it if the VPN connection requires it. Otherwise simply leave it empty.

By default you can configure the script to add up to 5 routes (more than enough for my needs).
But you can add more with a simple tuning of the file.

The ROUTE# variables need to contain the actual route command used to add the route.
For example:
SET ROUTE1=route add 10.110.0.0 mask 255.255.0.0 10.255.255.2

(See the syntax of the route command for further explanations).

The batch file works as follows:
1 - Connect the VPN with the credentials supplied in the USER VARIABLES section.
2 - If the connection is successful then loop trough all the connections to check for the interface id. This only work properly on more recent versions of Windows as I mentioned earlier. Windows XP for instance uses an hexadecimal notion for the interface ID which totally messes things up.
3 - Go through the ROUTE# variables, add the interface id to the route command and set the route.
4 - Pause until user presses a key.


The batch file doesn't disconnect the VPN automatically, but if you take a look at it you can see this option is commented out since I was accidently pressing a key and sending the VPN connection down too often. :D

In the (unlikely) case you need to add more routes you can add another ROUTE# variable and then duplicate on the "Set route" sections on the last part of the file:

echo . 
if defined ROUTE# (
    set CMD=%ROUTE#% if %IFNUMBER%
    %CMD%
    IF %ERRORLEVEL% GEQ 1 (
        echo Error Setting %ROUTE#%!
        pause
        goto :disconnect
    )
)


Just replace the 3 references to the ROUTE# variable with your one variable.


Hope you find this useful!


2 comments:

  1. Hi, You can add the following URL: http://marcosgildavid.blogspot.com/feeds/posts/default
    It shows up on the footer for the mainpage.
    Hope it helps!

    ReplyDelete
  2. Nice script.Good solution.
    top10-bestvpn.com

    ReplyDelete