Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

In an XP Pro workstation, is there a way to start the native Windows VPN client and open/close a connection from the command line so it can be scripted in a batch file?

share|improve this question

4 Answers 4

up vote 24 down vote accepted

Yes, if the VPN connection is called "My VPN" then:

rasdial "My VPN"

will dial the connection. Helpfully it sets errorlevel to the RAS error code if it fails to connect, so your script can detect a connection failure. If you need to supply a username and password instead of using the saved credentials use:

rasdial "My VPN" username password

To disconnect a connection use:

rasdial "My VPN" /disconnect

JR

share|improve this answer
    
Thanks, this looks like it will do exactly what I need. –  nedm Jun 22 '09 at 7:20
1  
+1. This is exactly what I am looking for in my build automation scripts. A lot easier than I anticipated. –  joseph.ferris Oct 9 '09 at 17:03
    
FWIW, also works in Vista (as expected) –  daughtkom Jan 23 '10 at 14:48
    
See EM's answer if you're on Windows 7. You may need to use rasphone if rasdial doesn't work for you. –  ADTC Mar 16 at 2:26

An alternative that works under Windows 7 (not sure about XP) is:

rasphone -d "My VPN"

This brings up the "dialling" dialog, same as if you double-click on the connection. If you have the username and password saved it automatically dials.

rasdial did not work for me (on Windows 7):

Verifying username and password...

Remote Access error 691 - The remote connection was denied because the user name
 and password combination you provided is not recognized, or the selected authen
tication protocol is not permitted on the remote access server.

If you add empty strings under Windows 7 it works with cached credentials: rasdial "My VPN" "" "" (those are two pairs of double quotes with nothing in between)

share|improve this answer
    
I couldn't get rasdial to work on Windows 8.1 but this worked perfectly. +1 –  Smalltown2k May 3 '14 at 16:54
    
I had to provide explicit credentials to get it to work on Windows 8.1. –  ShadowXVII Jan 14 at 6:15
    
I get the same error using rasdial with or without the empty strings. rasphone -d works. One thing to note is that if you run it from a command line manually, it returns to the command line immediately after executing (the connection connects in parallel). But if you place the command in a batch script (.bat) file and run the .bat file, it works serially - that is, it doesn't return control to the batch until the connection is complete. The difference could be confusing to a new user testing the command out. –  ADTC Mar 16 at 2:21

Run command-line: Control ncpa.cpl

enter image description here

Network Connections control

Network Connections control

In first run, edit My VPN Settings

enter image description here

After setup, edit My VPN Settings

enter image description here

Save credentials

enter image description here

Destination VPN host settings

enter image description here

Silent VPN up settings

enter image description here

VPN networking

enter image description here

VPN TCP advanced

enter image description here

VPN gateway

enter image description here

Advanced network connection

enter image description here

Set priority interface

enter image description here

Set priority interface apply

enter image description here

Set priority network provider

enter image description here

Script Silent-dial.cmd:

:: - comment in cmd) - REM alternative
:: disabled command output

@echo off

:: Silent dial "My VPN"
@rasphone -d "My VPN"

:: wait 10 sec W2K3 server test
::@SET waitsec=10
::@choice /T %waitsec% /N /D y /M "wait %waitsec% sec"

:: wait 10 sec - alternative - XP .. 7
@ping 127.0.0.1 -n 10 > NUL 

:: ********************************************
:: get path
:: set route table
:: run application 
:: ********************************************

@ping 127.0.0.1 -n 10 > NUL 

:: silent close "My VPN" connection
@rasphone -h "My VPN"

:: END Silent-dial.cmd

Use powershell or WSH.

share|improve this answer
    
Please try to add descriptions to your code-only answers :) –  Adrian Heine Nov 23 '12 at 19:37

I have not seen a command line scheme for the client yet. But, that does not mean it cannot be automated in scripts. Here is a two step approach,

  • Create the shortcut link for the VPN as is done normally for your VPN users
    • Keep it configured with username and password
  • Use AutoIt to script only,
    • launching the shortcut, and
    • pressing ENTER on the VPN login window (which is what a user does when everything is configured)


If I recollect correctly, the code for pressing enter on a window is simply,

Send ("{ENTER}")

Look at the Send command.

You can setup AutoIt on one of your administration machines, get the script working, make and executable for it, and give it to the users. It can then be launched from command line as an executable.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.