Search code examples
windowscmdvpnstartupopenvpn

OpenVPN Connection on startup (Windows) only if not at home


I want my OpenVPN to automatically connect on startup, but only if I am not at home. With some help of this community I was able to put together a batch script, that is supposed to do so, but it keeps throwing the error "-connect" could not be parsed as an argument because no "--" could be found, but there is a "--". Where is my mistake?

@echo off
set "SSID=Home_Network_SSID"
(netsh wlan show networks mode=ssid | findstr /C:"%SSID%" >nul 2>&1)&&goto :Home
echo "Nicht zu Hause, VPN wird verbunden...
start "OpenVPN" "C:\Program Files\OpenVPN\bin\openvpn-gui.exe" --connect profile.ovpn
timeout /T 5
echo "VPN wurde gestartet"
goto :end

:Home
echo "Heimnetzwerk erkannt, VPN wird nicht verbunden."
timeout /T 5
goto :end

In addition, it also fires the query to the client if I am at home, so it does not really work. If I put

"C:\Program Files\OpenVPN\bin\openvpn-gui.exe --connect profile.ovpn"

directly into cmd, there is no problem.

Please do not wonder about the german echos. Thank you very much,

DarkDiamond


Solution

  • I did even more research on this topic, and found something very useful that I use now instead of my own little batch file as it does what I want it to do, and that always when I connect to a wifi, and does not only check on startup. Using this task from within the windows task scheduler, it runs in the background, and I never had to think about it since I configured it.

    These 2 xml files have to be edited exactly how the installation instructions describe it, and then it works just fine for my needs. There is also the possibility to disable all popups, but in order for this to work, you need your password for the OpenVPN connection to be stored in plain text next to the config file, which I don't want to do. If you do this, you have to be very careful, whoever gets his hand on both of these files will be able to connect to your vpn server (which is probably in your home LAN behind all firewalls!) with your credentials and thus can steal data or even place viruses throughout the network. Considering this, I would definitely not recommend to use this option and rather type in the password manually every time the laptop connects to a different wifi than your home's.

    Also the second task, which handles the disconnection whenever your disconnect from a wifi works well. It just kills every task with "openvpn" in it's name, so you are good to go for the next connection.

    I did not figure this out by myself, all credits go to Carlin Scott who wrote the tasks. I just wanted to share my results with you as you helped me very much with the first solution.