2

Currently, you have to manually connect to a remote database to extract info via an openvpn connection openvpn-gui.exe to extract the info and disconnect after each extraction job.

Connection is authenticated by a config.ovpn file stored locally.

Is there a way to automate the (connect > extract data > disconnect) process?

JoshZ
  • 301
  • 1
  • 2
  • 12
  • 1
    Whatever you come up with, please realise that part of the process of connecting manually is providing credentials (a password most likely). If you automate it, you will be storing your password in a potentially unsafe location, which may be a very serious security issue, as the OpenVPN connection into whatever network you're getting into may allow for other access than just this database you need to get to. – Grismar Oct 22 '21 at 01:14
  • Thanks for the headsup! The config.ovpn file is stored locally (private machine) so directing the verification to a local file should not post any problems. – JoshZ Oct 22 '21 at 01:19

1 Answers1

3

Managed to solve this issue...

  1. Log in to your openvpn server domain via browser (e.g. https://12.345.678.999/)

  2. Download Connection Profile "Yourself (autologin profile)". File usually named "client.ovpn"

    • IMPT! File holds user id and password credentials. Although file is kept on local machine, credentials file can be copied if pc is hacked/stolen

      enter image description here

  3. Paste "client.ovpn" file in "C:\Program Files\OpenVPN\config"

  4. From openvpn-gui.exe desktop icon, import file and direct it to "client.ovpn"

enter image description here

  1. Create 2 notepad files with the following commands and save as batch file (xxx.bat)

ovpn_connect.bat

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

ovpn_disconnect.bat

"C:\Program Files\OpenVPN\bin\openvpn-gui.exe" --command disconnect client.ovpn
  1. Follow website instruction to allow .bat file to be run with admin rights.

  2. Insert code into python script and run as per usual

import subprocess, time

# Connect to OpenVPN
subprocess.call([r'filepath\ovpn_connect.bat'])
time.sleep(15) # adjust your connection time
print("Connect OpenVPN")

# Disconnect from OpenVPN
subprocess.call([r'filepath\ovpn_disconnect.bat'])
print("Disconnect OpenVPN")
JoshZ
  • 301
  • 1
  • 2
  • 12