2010-07-19 13:21:10Sam Blog

VPN Batch Script

Here is a batch script I made to log me on/off of a vpn server and also mount/unmount a network drive.

I thought it would be useful to share

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::: VPN.BAT OVERVIEW :::
::: This script automatically connects to a VPN Server and mounts :::
::: a network drive for the connection. If a connection is already :::
::: established, the script unmounts the drive and terminates the :::
::: connection. :::
::: :::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::: INSTRUCTIONS :::
::: To configure the script, edit your username, password, and the :::
::: connection name below. These are the criteria which let you :::
::: log into your vpn server. Next set the Drive you want to mount:::
::: as the DRIVE variable (eg. h:) and the location of the mount in :::
::: the DRIVEMAP Variable. Run the script by double clicking. :::
::: :::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::: SECURITY :::
::: Due to the nature of batch files, anyone who has access to the :::
::: file can also read the contents (most notably your username and :::
::: password). I would recommend using a program to compile this :::
::: batch into an EXE. I have tested this method using "Quick Batch :::
::: File Compiler" (http://www.abyssmedia.com/quickbfc/) and "Batch :::
::: File Compiler Pro" (http://www.bdargo.com/). This method will :::
::: at least remove your username and password from plain sight. :::
::: :::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::: CREDITS :::
::: CREATED BY : Jesse Bowes :::
::: LAST MODIFIED : September 13, 2006 :::
::: WEBSITE : http://www.jessebowes.com :::
::: :::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:************* EDIT THESE *************
@SET USERNAME=username
@SET PASS=pass
@SET CONNNAME=as defined in 'Network Connections'
@SET DRIVE=h: (drive you want to mount)
@SET DRIVEMAP=\\server\username (location of mount)
:************* END EDIT *************

@echo *** VPN CONNECTION UTILITY ***

@rasdial > c:\tempfile
@set /p STATUS= < c:\tempfile
@del c:\tempfile

@IF "%status%" == "No connections" GOTO MAKE
@GOTO CLOSE

:MAKE
@echo *** Establishing VPN connection ***
@rasdial %CONNNAME% %USERNAME% %PASS%
@echo *** Mapping Network drive ***
@PING 1.1.1.1 -n 1 -w 6000 >NUL
net use %DRIVE% %DRIVEMAP%

@GOTO END

:CLOSE
@echo *** Terminating VPN connection ***
@net use h: /delete
@rasdial %CONNNAME% /disconnect
@GOTO END

:END
@set STATUS=
@SET USERNAME=
@SET PASS=
@SET CONNNAME=
@SET DRIVE=
@SET DRIVEMAP=
pause