Establish a VPN connection in cmd

162,149

Solution 1

I know this is a very old thread but I was looking for a solution to the same problem and I came across this before eventually finding the answer and I wanted to just post it here so somebody else in my shoes would have a shorter trek across the internet.

****Note that you probably have to run cmd.exe as an administrator for this to work**

So here we go, open up the prompt (as an adminstrator) and go to your System32 directory. Then run

C:\Windows\System32>cd ras

Now you'll be in the ras directory. Now it's time to create a temporary file with our connection info that we will then append onto the rasphone.pbk file that will allow us to use the rasdial command.

So to create our temp file run:

C:\Windows\System32\ras>copy con temp.txt

Now it will let you type the contents of the file, which should look like this:

[CONNECTION NAME]
MEDIA=rastapi
Port=VPN2-0
Device=WAN Miniport (IKEv2)
DEVICE=vpn
PhoneNumber=vpn.server.address.com

So replace CONNECTION NAME and vpn.server.address.com with the desired connection name and the vpn server address you want.

Make a new line and press Ctrl+Z to finish and save.

Now we will append this onto the rasphone.pbk file that may or may not exist depending on if you already have network connections configured or not. To do this we will run the following command:

C:\Windows\System32\ras>type temp.txt >> rasphone.pbk

This will append the contents of temp.txt to the end of rasphone.pbk, or if rasphone.pbk doesn't exist it will be created. Now we might as well delete our temp file:

C:\Windows\System32\ras>del temp.txt

Now we can connect to our newly configured VPN server with the following command:

C:\Windows\System32\ras>rasdial "CONNECTION NAME" myUsername myPassword

When we want to disconnect we can run:

C:\Windows\System32\ras>rasdial /DISCONNECT

That should cover it! I've included a direct copy and past from the command line of me setting up a connection for and connecting to a canadian vpn server with this method:

Microsoft Windows [Version 6.2.9200]
(c) 2012 Microsoft Corporation. All rights reserved.

C:\Windows\system32>cd ras

C:\Windows\System32\ras>copy con temp.txt
[Canada VPN Connection]
MEDIA=rastapi
Port=VPN2-0
Device=WAN Miniport (IKEv2)
DEVICE=vpn
PhoneNumber=ca.justfreevpn.com
^Z
        1 file(s) copied.

C:\Windows\System32\ras>type temp.txt >> rasphone.pbk

C:\Windows\System32\ras>del temp.txt

C:\Windows\System32\ras>rasdial "Canada VPN Connection" justfreevpn 2932
Connecting to Canada VPN Connection...
Verifying username and password...
Connecting to Canada VPN Connection...
Connecting to Canada VPN Connection...
Verifying username and password...
Registering your computer on the network...
Successfully connected to Canada VPN Connection.
Command completed successfully.

C:\Windows\System32\ras>rasdial /DISCONNECT
Command completed successfully.

C:\Windows\System32\ras>

Hope this helps.

Solution 2

Have you looked into rasdial?

Just incase anyone wanted to do this and finds this in the future, you can use rasdial.exe from command prompt to connect to a VPN network

ie rasdial "VPN NETWORK NAME" "Username" *

it will then prompt for a password, else you can use "username" "password", this is however less secure

http://www.msfn.org/board/topic/113128-connect-to-vpn-from-cmdexe-vista/?p=747265

Solution 3

Is Powershell an option?

Start Powershell:

powershell

Create the VPN Connection: Add-VpnConnection

Add-VpnConnection [-Name] <string> [-ServerAddress] <string> [-TunnelType <string> {Pptp | L2tp | Sstp | Ikev2 | Automatic}] [-EncryptionLevel <string> {NoEncryption | Optional | Required | Maximum}] [-AuthenticationMethod <string[]> {Pap | Chap | MSChapv2 | Eap}] [-SplitTunneling] [-AllUserConnection] [-L2tpPsk <string>] [-RememberCredential] [-UseWinlogonCredential] [-EapConfigXmlStream <xml>] [-Force] [-PassThru] [-WhatIf] [-Confirm] 

Edit VPN connections: Set-VpnConnection

Set-VpnConnection [-Name] <string> [[-ServerAddress] <string>] [-TunnelType <string> {Pptp | L2tp | Sstp | Ikev2 | Automatic}] [-EncryptionLevel <string> {NoEncryption | Optional | Required | Maximum}] [-AuthenticationMethod <string[]> {Pap | Chap | MSChapv2 | Eap}] [-SplitTunneling <bool>] [-AllUserConnection] [-L2tpPsk <string>] [-RememberCredential <bool>] [-UseWinlogonCredential <bool>] [-EapConfigXmlStream <xml>] [-PassThru] [-Force] [-WhatIf] [-Confirm]

Lookup VPN Connections: Get-VpnConnection

Get-VpnConnection [[-Name] <string[]>] [-AllUserConnection]

Connect: rasdial [connectionName]

rasdial connectionname [username [password | \]] [/domain:domain*] [/phone:phonenumber] [/callback:callbacknumber] [/phonebook:phonebookpath] [/prefixsuffix**]

You can manage your VPN connections with the powershell commands above, and simply use the connection name to connect via rasdial.

The results of Get-VpnConnection can be a little verbose. This can be simplified with a simple Select-Object filter:

Get-VpnConnection | Select-Object -Property Name

More information can be found here:

Share:
162,149

Related videos on Youtube

user1892542
Author by

user1892542

Updated on March 17, 2020

Comments

  • user1892542
    user1892542 about 4 years

    How can I create a VPN connection with an arbitrary server using an arbitrary protocol in Windows cmd?

  • user1892542
    user1892542 over 11 years
    yes,but rasdial is for connecting to existing connection,I need to create connection
  • Utsav Gupta
    Utsav Gupta over 9 years
    I get the following error: Remote Access error 623 - The system could not find the phone book entry for th s connection.
  • Utsav Gupta
    Utsav Gupta over 9 years
    my rasphone.pbk looks like this [DEV_QA] MEDIA=rastapi Port=VPN2-0 Device=WAN Miniport (IKEv2) Device=vpn PhoneNumber=XXX
  • n611x007
    n611x007 about 9 years
    FYI when I was missing the rasphone.pbk file, I got the this function is only valid in win32 mode error. (this is just an informational comment, later I created the file and it worked as expected.)
  • keshav
    keshav over 8 years
    I am getting the following error: Remote Access error 800 - The remote connection was not made because the attempted VPN tunnels failed. The VPN server might be unreachable. If this connection is attempting to use an L2TP/IPsec tunnel, the security parameters required for IPsec negotiation might not be configured properly.
  • John Dorian
    John Dorian over 8 years
    @keshav I would imagine you have an issue with your VPN server. Have you tried this with a server that you've verified works?
  • keshav
    keshav over 8 years
    I tried with cisco anyconnect ui and it worked. I wanted to automate vpn connection using above given solution but it is showing 800 error
  • Daniel Liuzzi
    Daniel Liuzzi almost 8 years
    The question asks how to establish a VPN connection.
  • Juan Acosta
    Juan Acosta almost 8 years
    This command have some issues in Windows 10. I tried and it say connected successfully. However, if you try to Get-VpnConnection using powershell it will tell you not connected. I think it is probably a bug in the latest version of windows.
  • oobe
    oobe almost 8 years
    Thanks a lot! Works good but folder for rasphone.pbk in %userprofile%\AppData\Roaming\Microsoft\Network\Connections\‌​PBK
  • The Red Pea
    The Red Pea about 7 years
  • Brethlosze
    Brethlosze over 6 years
    Worked on Win8. The location is in %userprofile%\AppData\Roaming\Microsoft\Network\Connections\‌​‌​PB as pointed above. I had the VPN configured previously so actually i didnt required any particular edition, just the rasdial command...
  • alexsuslin
    alexsuslin about 6 years
    i see everything except how to connect =)
  • Ali Karaca
    Ali Karaca almost 6 years
    Using as rasdial "vpnName" also works like a charm in cmd. Thanks for answer!
  • B. Shea
    B. Shea over 2 years
    All of this because MS can't make a tray icon to control the VPN by a simple click (connect/disconnect). Amazing .. per usual. MS: "Let's make it extra hard to be secure."? 3rd party software always seems to manage it. See OpenVPN/GUI, NetExtender etc.