Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

batch file to connect and disconnect from vpn connection

connect.bat

rasdial myvpn

disconnect.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:

if(connected)
   disconnect
else
   connect
like image 415
derfect Avatar asked Aug 13 '15 14:08

derfect


People also ask

How do I get my VPN to automatically connect?

On AndroidTap on the Settings icon in the upper-left corner and choose VPN connection. Tap Auto-connect. Select when you want to establish a VPN connection automatically. Tap Auto-connect to and select the server you want to connect automatically.

How do I connect to VPN using Powershell?

This does technically work, but rasdial requires you enter the username and password in this format: rasdial $vpnName $Username $password.... Even if the password is saved, it requires you to have those parameters in plaintext.


1 Answers

  • Based on ping in 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 /all when connected) and put this in your batch file or directly in the shortcut properties prepending with cmd /c in the latter case.

  • Based on connection name:

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

    Replace myvpn with your VPN connection name

like image 138
wOxxOm Avatar answered Sep 20 '22 17:09

wOxxOm