windows 在cmd中建立VPN连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14614465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Establish a VPN connection in cmd
提问by user1892542
How can I create a VPN connection with an arbitrary server using an arbitrary protocol in Windows cmd
?
如何在 Windows 中使用任意协议创建与任意服务器的 VPN 连接cmd
?
回答by John Dorian
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**
****请注意,您可能必须以管理员身份运行 cmd.exe 才能使其工作**
So here we go, open up the prompt (as an adminstrator) and go to your System32directory. Then run
所以我们开始了,打开提示(以管理员身份)并转到您的System32目录。然后运行
C:\Windows\System32>cd ras
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.pbkfile that will allow us to use the rasdial command.
现在您将位于 ras 目录中。现在是时候用我们的连接信息创建一个临时文件,然后我们将其附加到rasphone.pbk文件中,这将允许我们使用 rasdial 命令。
So to create our temp file run:
因此,要创建我们的临时文件,请运行:
C:\Windows\System32\ras>copy con temp.txt
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.
因此,将 CONNECTION NAME 和 vpn.server.address.com 替换为所需的连接名称和所需的 vpn 服务器地址。
Make a new line and press Ctrl+Z to finish and save.
新建一行并按 Ctrl+Z 完成并保存。
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:
现在我们将把它附加到 rasphone.pbk 文件中,该文件可能存在也可能不存在,这取决于您是否已经配置了网络连接。为此,我们将运行以下命令:
C:\Windows\System32\ras>type temp.txt >> rasphone.pbk
C:\Windows\System32\ras> 输入 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:
这会将 temp.txt 的内容附加到 rasphone.pbk 的末尾,或者如果 rasphone.pbk 不存在,它将被创建。现在我们不妨删除我们的临时文件:
C:\Windows\System32\ras>del temp.txt
C:\Windows\System32\ras>del temp.txt
Now we can connect to our newly configured VPN server with the following command:
现在我们可以使用以下命令连接到我们新配置的 VPN 服务器:
C:\Windows\System32\ras>rasdial "CONNECTION NAME" myUsername myPassword
C:\Windows\System32\ras>rasdial "CONNECTION NAME" myUsername myPassword
When we want to disconnect we can run:
当我们想断开连接时,我们可以运行:
C:\Windows\System32\ras>rasdial /DISCONNECT
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:
那应该涵盖它!我已经从我的命令行中包含了一个直接副本和过去,使用此方法设置连接并连接到加拿大 vpn 服务器:
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.
希望这可以帮助。
回答by LNendza
Have you looked into rasdial?
你研究过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
以防万一有人想这样做并在将来发现这一点,您可以从命令提示符使用 rasdial.exe 连接到 VPN 网络
IE
rasdial "VPN NETWORK NAME" "Username" *
然后它会提示输入密码,否则你可以使用“用户名”“密码”,但这不太安全
http://www.msfn.org/board/topic/113128-connect-to-vpn-from-cmdexe-vista/?p=747265
http://www.msfn.org/board/topic/113128-connect-to-vpn-from-cmdexe-vista/?p=747265
回答by éliette
Is Powershell an option?
Powershell 是一种选择吗?
Start Powershell:
启动 Powershell:
powershell
Create the VPN Connection: Add-VpnConnection
创建 VPN 连接: 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
编辑 VPN 连接: 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
查找 VPN 连接: Get-VpnConnection
Get-VpnConnection [[-Name] <string[]>] [-AllUserConnection]
Connect: rasdial [connectionName]
连接: 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
.
您可以使用上面的 powershell 命令管理您的 VPN 连接,只需使用连接名称即可通过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
过滤器来简化:
Get-VpnConnection | Select-Object -Property Name
More information can be found here:
更多信息可以在这里找到: