JoshZ
JoshZ

Reputation: 321

OpenVPN Connection Using Python (Windows)

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?

Upvotes: 3

Views: 17810

Answers (1)

JoshZ
JoshZ

Reputation: 321

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")

Upvotes: 4

Related Questions