windows 用于连接和断开 VPN 连接的批处理文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31990775/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:13:20  来源:igfitidea点击:

batch file to connect and disconnect from vpn connection

windowsbatch-filecmd

提问by derfect

connect.bat

连接.bat

rasdial myvpn

disconnect.bat

断开连接.bat

rasdial myvpn /DISCONNECT

....I assigned those two files a keyboard shortcut to run them and they work perfectly.

....我为这两个文件分配了一个键盘快捷键来运行它们,它们运行良好。



QUESTION: is it possible to make a single .bat that does the following:

问题:是否可以制作一个执行以下操作的单个 .bat:

if(connected)
   disconnect
else
   connect

回答by wOxxOm

  • Based on pingin case the VPN server IP is always the same:

    ping -n 1 1.2.3.4 && rasdial myvpn /disconnect || rasdial myvpn
    

    Replace 1.2.3.4 with your VPN server ip (use ipconfig /allwhen connected) and put this in your batch file or directly in the shortcut properties prepending with cmd /cin the latter case.

  • Based on connection name:

    ipconfig|find/i "myvpn" && rasdial myvpn /disconnect || rasdial myvpn
    

    Replace myvpnwith your VPN connection name

  • 基于pingVPN 服务器 IP 始终相同的情况:

    ping -n 1 1.2.3.4 && rasdial myvpn /disconnect || rasdial myvpn
    

    将 1.2.3.4 替换为您的 VPN 服务器 ip(ipconfig /all连接时使用)并将其放入批处理文件中或直接放入cmd /c后一种情况下前缀为 的快捷方式属性中。

  • 基于连接名称:

    ipconfig|find/i "myvpn" && rasdial myvpn /disconnect || rasdial myvpn
    

    替换myvpn为您的 VPN 连接名称