Establish a VPN connection in cmd

后端 未结 3 1805
渐次进展
渐次进展 2020-12-13 13:10

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

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-13 13:15

    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.

提交回复
热议问题