Skip to content

Instantly share code, notes, and snippets.

@alexmacniven
Last active January 4, 2022 10:35
Show Gist options
  • Save alexmacniven/5644074f68d8a53fad7bdd4484fd851b to your computer and use it in GitHub Desktop.
Save alexmacniven/5644074f68d8a53fad7bdd4484fd851b to your computer and use it in GitHub Desktop.
Connecting to and disconnecting from a VPN with OpenVPN and Surfshark configurations

Command Line OpenVPN

This document outlines my findings after hacking with OpenVPN.

Download OpenVPN

I had issues with the latest release of OpenVPN Connect but version 2.5.2 worked fine;

Download Link

Download and install with the default parameters.

Surfshark Credentials and Configurations

Head over to your Surfshark settings page and grab the username and password from the credentials tab.

Whilst here, head to the locations tab and download a configurations file in .udp format

Next create an auth.txt file and add your username and password (from above) on seperate lines e.g.

myusername
mypassword

With your configurations file you need to open it in your text editor and add the line auth-user-pass auth.txt

To keep things tidy lets move auth.txt and the .ovpn file to the OpenVPN directory at C:\Program Files\OpenVPN\bin

Starting OpenVPN

Now we're all set up you can open a Powershell console with Administrator privilidges (very important) and change directory to C:\Program Files\OpenVPN\bin

Assuming the application is installed correctly you can connect to the VPN with the Start-Process command

(We'll assume my .ovpn file is be-bru.prod.surfshark.comsurfshark_openvpn_udp.ovpn)

Start-Process -FilePath "openvpn.exe" -ArgumentList "--config surfshark.comsurfshark_openvpn_udp.ovpn" -WorkingDirectory "C:\Program Files\OpenVPN\bin\"

You should see a console pop-up window with the OpenVPN logs 🥳

Stopping OpenVPN

To stop the service and close the connection you can simply close pop-up window that was created.

Or you may find it more benefical to stop the process using the Stop-Process command

Stop-Process -Name openvpn

Doing It With Python

So we can easily start and quit our VPN connection with Powershell but how about with Python?

After many failed attempts with subprocess.Popen I finally had some luck with invoking a Powershell script from Python.

It requires a bit of extra setup.

First lets create start.ps1, it simply contains the Start-Process command from previous.

Start-Process -FilePath "openvpn.exe" -ArgumentList "--config surfshark.comsurfshark_openvpn_udp.ovpn" -WorkingDirectory "C:\Program Files\OpenVPN\bin\"

Then create stop.ps1, which contains the Stop-Process command.

Stop-Process -Name openvpn

Let's invoke these from a Python console, but first you're going to need the path to your powershell.exe.

>>> import subprocess
>>> psexe = 'C:\\Program Files\\PowerShell\\7\\pwsh.exe'
>>> subprocess.run([exe, "start.ps1"])  # Start the VPN connection.
CompletedProcess(args=['C:\\Program Files\\PowerShell\\7\\pwsh.exe', 'start.ps1'], returncode=0)
>>> subprocess.run([exe, "stop.ps1"])  # Stop the VPN connection.
CompletedProcess(args=['C:\\Program Files\\PowerShell\\7\\pwsh.exe', 'stop.ps1'], returncode=0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment