Sunday, 8 February 2009

Automating VPN connections with Batch Files

If you frequently need to connect to your office from home or away via a VPN, I have listed below a simple batch file that utilises rasdial.exe to make the connection and disconnection with one click. It also uses mstsc.exe to connect to the server using Remote Desktop (Terminal Services client). The error handling makes sure that you are aware of any errors but otherwise it is completely automatic. After finishing the Terninal Services session the VPN is automatically disconnected.

All these programs and utilities are standard in Microsoft XP Professional.

@echo off
rasdial YourVPN mike secretpassword
if NOT %ERRORLEVEL% ==0 goto failed
mstsc OurServer.rdp
rasdial YourVPN /DISCONNECT
if NOT %ERRORLEVEL% ==0 goto failed2
exit

:failed
echo.
echo Failed to connect to VPN!
pause
exit

:failed
echo.
echo Failed to disconnect from VPN!
pause
exit

Notes on the batch file
  1. In the batchfile replace YourVPN with the exact name of the VPN that you need to create in Network Connections
  2. Replace mike with your username for the VPN
  3. Replace secretpassword with your VPN password
  4. Replace OurServer.rdp with the exact name of the Remote Desktop connection that you need to create in Start, All Programs, Accessories, Communications, Remote Desktop Connection.
Create the batch file by 'right-clicking' in a folder and selecting New.. Text Document This will prompt you for a name. Call it Connect.bat. Copy and paste the code above and save it.

For more details on rasdial.exe click on the link below

No comments:

Post a Comment