6

I need to be able to turn on my vpn in a python script and then terminate it. Very easy to do it manually (see picture in the link below) but I have no idea how to code it. I heard about subprocess.Popen but not sure if I am on the right track.

manual way of turning on my vpn

I am using Ubuntu 16.04 and my VPN is TrustZone.

Thank you for your help.

Charles

Charles Verleyen
  • 157
  • 1
  • 1
  • 5
  • 1
    Unless you post any code no one will be able to tell you if you are on the right track. VPN provider is not so important, configuration on the other hand is. What have you tried? – gonczor Feb 12 '18 at 20:25
  • @AfroThundr : Thank you very much. The command 'nmcli' is working great to launch the VPN connection. – Charles Verleyen Feb 13 '18 at 07:16

2 Answers2

7

I've been working on something similar and it work fine with python on Debian and Ubuntu, It depend on openvpn So make sure to install openvpn in your machine using :

Sudo apt-get update
Sudo apt-get install openvpn

Then you can use this small peace of python code (vpn.py) to run the vpn make sure you use the sudo and before run it use the chmod 777 on the file. In your case you're using trustzone make sure to generate the config file with the extension .ovpn

https://trust.zone/setup/ubuntu/ovpn/za

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    import requests, os, sys, subprocess, time
    path = '/home/user/Download/trustedzone.ovpn'
    with open("/home/user/Download/trustedzone.ovpn", "a") as myfile:
        myfile.write('\nscript-security 2\nup /etc/openvpn/update-resolv-conf\ndown /etc/openvpn/update-resolv-conf')
        myfile.close()
x = subprocess.Popen(['sudo', 'openvpn', '--auth-nocache', '--config', path])
    try:
        while True:
            time.sleep(600)
    # termination with Ctrl+C
    except:
        try:
            x.kill()
        except:
            pass
        while x.poll() != 0:
            time.sleep(1)

Place The script where you want to run it then use the command

Sudo chmod 777 vpn.py

To start The vpn client Run

Sudo ./vpn.py

Wish it will work for you, have a good journey.

  • 1
    Why do we need "'\nscript-security 2\nup /etc/openvpn/update-resolv-conf\ndown /etc/openvpn/update-resolv-conf'". Its giving me no such file/directory error for "update-resolv-conf" – amalik2205 Nov 29 '19 at 15:53
  • I am also a bit surprised by the the part that writes to the config file. It seems it is appending some lines every time you run the script. If you need to modify the ovpn file for this to work, isn't it easier to do this once by hand? – Bas Swinckels Aug 29 '22 at 06:27
2

Taking a wild stab from that screenshot, your VPN appears to be configured using NetworkManager. In that case, the following commands would start and stop your VPN:

import os

os.system('nmcli c up <VPN_NAME>')    # Start the VPN
os.system('nmcli c down <VPN_NAME>')  # Stop the VPN

You can find more info on running system commands from the interpreter here, and on using NetworkManager commands here.

AfroThundr
  • 1,175
  • 2
  • 17
  • 28
  • Is there any way to insert your vpn host address and username and password ? – Taha Hamedani May 31 '20 at 06:15
  • 1
    @TahaHamedani You would usually embed that information in the NetworkManager connection profile. If this info is in an `.ovpn` file, you can import it [like so](https://unix.stackexchange.com/q/140163/). – AfroThundr May 31 '20 at 17:27
  • Thanks for your reply, I actually want to set up a VPN pool with host address and username and password in my python script and using GUI for this purpose is not my desire. Do you have any solution for this scenario? – Taha Hamedani Jun 01 '20 at 05:53