Batch/CMD stops after OpenVPN command line

3.8k views Asked by At

I'm trying to create an auto connect and disconnect file with the OpenVPN and in addition to that, set my WiFi adapters DNS to 0.0.0.0 (becuase of DNS leaks) and back to normal.

Here's my current code:

@echo off
SETLOCAL EnableExtensions
set EXE=openvpn-gui.exe
FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto NOT
goto RUNS

:NOT
taskkill /im openvpn-gui.exe /f
taskkill /im openvpn.exe /f
netsh interface IPv4 set dnsserver "WiFi" dhcp
ipconfig /flushdns
exit

:RUNS
cd "C:\Program Files\OpenVPN\bin"
openvpn-gui.exe --connect "vpn-connection-file.ovpn" 

netsh interface IPv4 set dnsserver "WiFi" static 0.0.0.0 both
ipconfig /flushdns
exit

The first lines of my codes checks if openVPN is running, if it does, go to RUN, else to NOT.

NOT is ending OpenVPN and sets DNS back to normal.

RUN is opening OpenVPN and esatblishes a connection to my config file.

However, after this line:

openvpn-gui.exe --connect "vpn-connection-file.ovpn" 

CMD won't continue with the rest of the codes. It just stops, and the white space bar is blinking. (Not possible to enter any keys or whatever.) Checking if the rest of the codes have been executed is negative.

Anybody know whats going on here?

1

There are 1 answers

2
MatSnow On BEST ANSWER

CMD waits for openvpn-gui.exe to end. Try it with start

start openvpn-gui.exe --connect "vpn-connection-file.ovpn"