Client connect/disconnect scripting on Windows 7

How to customize and extend your OpenVPN installation.

Moderators: TinCanTech, TinCanTech, TinCanTech, TinCanTech, TinCanTech, TinCanTech

Post Reply
BobAGI
OpenVPN Power User
Posts: 167
Joined: Mon May 05, 2014 10:17 pm

Client connect/disconnect scripting on Windows 7

Post by BobAGI » Mon Feb 05, 2018 9:21 am

I am currently using a script which runs nightly to move some backup data from my Win7 PC to a server.
In order to do that I am connecting a PPTP Windows VPN via a bat file in advance of running the backup job.
At the end of the job the VPN tunnel is disconnected. This has worked fine for years but now the PPTP VPN is about to be discontinued due to the age of the server running RRAS.
Instead there is now an OpenVPN server on the remote LAN, so I would like to be able to do the same as above but with OpenVPN.

But when I google this (as I thought pretty straightforward) problem it turns out that everywhere I look there seems to only be solutions for the connection and none for the disconnect....

Is it really so that with OpenVPN on Windows there is no way to automate both the connect and disconnect to an OpenVPN server via a bat file?
In my case the three bat files run as scheduled tasks with a set user ID.
They run in sequence at fixed times:
1) Connect VPN: rasdial <VPNProfileName> <username> <userpwd>
2) Run the backup job
3) Disconnect VPN: rasdial <VPNProfileName> /disconnect"

Is there anything similar available for OpenVPN?
Notice that I have a lot of different OVPN connections registered with OpenVpn-GUI so a solution tha connects a "default" connection will not cut it.
Neither can a "solution" where openvpn is killed because I have other connections also running, which need to continue to be connected.

TinCanTech
OpenVPN Protagonist
Posts: 11137
Joined: Fri Jun 03, 2016 1:17 pm

Re: Client connect/disconnect scripting on Windows 7

Post by TinCanTech » Mon Feb 05, 2018 1:03 pm

BobAGI wrote:
Mon Feb 05, 2018 9:21 am
Is it really so that with OpenVPN on Windows there is no way to automate both the connect and disconnect to an OpenVPN server via a bat file?
In Linux you send a kill signal to the process .. try googling that for windows.

BobAGI
OpenVPN Power User
Posts: 167
Joined: Mon May 05, 2014 10:17 pm

Re: Client connect/disconnect scripting on Windows 7

Post by BobAGI » Mon Feb 05, 2018 2:31 pm

Well, killing the openvpn process will likely also kill the legitimate other connections which should be kept...
But I take your reply as:
No, there is no function to cleanly disconnect an established connection except by user GUI interaction.

Too bad.

TinCanTech
OpenVPN Protagonist
Posts: 11137
Joined: Fri Jun 03, 2016 1:17 pm

Re: Client connect/disconnect scripting on Windows 7

Post by TinCanTech » Mon Feb 05, 2018 2:59 pm

I think you are confusing two different ideas ..

What exactly is it you are trying to close ?

If you need the same VPN for other traffic then closing a TCP session which goes via a VPN
has nothing to do with closing openvpn.

BobAGI
OpenVPN Power User
Posts: 167
Joined: Mon May 05, 2014 10:17 pm

Re: Client connect/disconnect scripting on Windows 7

Post by BobAGI » Mon Feb 05, 2018 6:48 pm

As I tried to describe it in my start post:
I want to move an existing nightly recurring task from using Windows PPTP VPN to OpenVpn.
The function uses the following different bat file functions executed with a time separation in the middle of the night each day:
1) Connect a VPN tunnel to the remote server LAN
2) Execute commands to transfer backup files from the local computer to a server on the remote LAN
3) Disconnect the tunnel connected in step #1

This works very easily using Windows rasdial but not so simple using OpenVPN.
The problem is step #3.

The computer that executes this task might hold a number of connected OpenVPN tunnels already towards other server LAN:s and these must not be disturbed.
So I think that killing the openvpn processes will also tear down the other tunnels.
And in any case, how can one do that from a Windows batch file or similar?

TinCanTech
OpenVPN Protagonist
Posts: 11137
Joined: Fri Jun 03, 2016 1:17 pm

Re: Client connect/disconnect scripting on Windows 7

Post by TinCanTech » Mon Feb 05, 2018 7:18 pm

BobAGI wrote:
Mon Feb 05, 2018 6:48 pm
I think that killing the openvpn processes will also tear down the other tunnels.
why would it ?

Post Reply