15

Is it possible to start a windows openvpn client to make it connect using a predefined config (.ovpn) by supplying program arguments using command prompt. Or specifying the arguments in the shortcut when opening from windows shortcut etc.

7 Answers 7

17

Solved it as below:

from windows command prompt-

enter image description here

This is going to start the opn vpn gui client directly connecting to the connection specified in the config.

1
  • 3
    Mind you that if the openvpn-gui.exe is already started above answer won't work.
    – Jan
    Mar 28, 2017 at 13:21
12

Mind you that if the openvpn-gui.exe is already started above answers won't work. Pay attention to the :run section from a little batch file I made to automatically start openVPN connection when not at home:

rem This script is fired from Task Scheduler (using Custom Event filter) when I am NOT at home (not connected to home network)
rem  so check if my home NAS is already pingable, because maybe old/previous OpenVPN connection is still open
rem    if not then start OpenVPN connection
rem    if yes than do nothing
ping -n 1 192.168.10.100 > testping.txt
findstr /r /c:"Reply from \d*.\d*.\d*.\d*.* bytes=\d*.*time[<=]\d*.* TTL=\d*" testping.txt
IF ERRORLEVEL 1 goto run
rem do nothing because NAS is pingable
goto finished
:run
rem be sure to kill previous (closed) openvpn process so reconnecting actually works!
taskkill.exe /F /IM openvpn.exe
taskkill.exe /F /IM openvpn-gui.exe
timeout 1
start /b "" "C:\Program Files\OpenVPN\bin\openvpn-gui.exe" --connect nas_at_home.ovpn
:finished
2
  • 1
    Thanks for an excelent answer but I went the extra step of exporting my Windows task into a gist: gist.github.com/carlin-q-scott/77cbb064c3c2e332af011714fb2aa585 Dec 21, 2018 at 1:59
  • 1
    Why not pipe the output of ping into findstr and then use || to check for (negative) outcome to do goto run? You could also scrap the finished label and use :eof instead. Last but not least I'd always recommend using %ProgramFiles% as opposed to the already expanded path. Oct 29, 2021 at 6:41
5

In additional to Flowerking's answer, you could specify the folder in which your ovpn file lives, perhaps to store private key data in user space. For that, use config_dir:

openvpn-gui.exe --connect "client.ovpn" --config_dir "C:\Users\Foo\Documents\protected_crypto_data"
4

Not sure if this was recently added but there's a "--command" option that can be used to send commands to a running gui instance.
So now you can do
openvpn-gui.exe --command connect config.ovpn

See the options for openvpn-gui.exe by going to the command line and running
openvpn-gui.exe --help

2
  • What OpenVpn version do you have? I don't see this command option in v2.4.2.
    – Stanislav
    Sep 24, 2020 at 21:48
  • OpenVpn v.2.4.7 has this option
    – Stanislav
    Sep 24, 2020 at 22:36
3

Here is a simple example, if you want to connect to more VPNs than one:

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

It won't work, if the openvpn-gui.exe is running.

2

I had the same problems, and also needed the script to not re-open the UI if it was already connected, and to wait for the connection to finish.

I couldn't find a good solution, so I wrote my own. It is here in case it is useful to others:

https://www.pretentiousname.com/miscsoft/index.html#ConnectOpenVPN

Windows Vista and above; free; includes C++ source code.

(There's also a similar, older tool there for the Windows built-in VPN client.)

Example usage, with the /verbose switch that outputs detailed info about what it is doing:

C:\> ConnectOpenVPN.exe /connect /adapter "OpenVPN" /config "MyVPN.ovpn"

ConnectOpenVPN: Checking state of "OpenVPN" network adapter...
ConnectOpenVPN: "OpenVPN" network adapter is not connected.
ConnectOpenVPN: Running: "C:\Program Files\OpenVPN\bin\openvpn-gui.exe" --command connect "MyVPN.ovpn"
ConnectOpenVPN: Waiting for OpenVPN GUI to appear...
ConnectOpenVPN: OpenVPN GUI found.
ConnectOpenVPN: Waiting for OpenVPN GUI to close...
ConnectOpenVPN: OpenVPN GUI closed.
ConnectOpenVPN: Waiting for/confirming VPN connection exists...
ConnectOpenVPN: Connected.
ConnectOpenVPN: CONNECT action finshed.

I hope it is useful to other people.

0

You can improve @Jan 's answer by changing:

ping -n 1 192.168.10.100 > testping.txt
findstr /r /c:"Reply from \d*.\d*.\d*.\d*.* bytes=\d*.*time[<=]\d*.* TTL=\d*" testping.txt

to:

ping 192.168.10.100 -n 1 | findstr /r /c:"Reply from \d*.\d*.\d*.\d*.* bytes=\d*.*time[<=]\d*.* TTL=\d*" && goto :finished

This way you don't need to create a temporary file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .