OpenVPN Linux auto-restart

From EnigmaGuy Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Intro

There are basically two files to install on linux

  • openvpn-restart.service
  • restart_openvpn.py

You can get these files from Github.com

Propably interesting is how these scripts works

#!/usr/bin/env python3
import os,time
def reachable():
    for i in range(0,3):
        time.sleep(60)
        if os.system('ping -c1 YOUR_VPN_SERVER_PRIVATE_IP 2>&1 > /dev/null') == 0:
            print('ping succesfull')
            return True
        print('host unreachable')
    return False



while(True):
    if reachable():
        continue
    print('restarting openvpn')
    os.system('systemctl restart openvpn@YOUR_CONFIG_NAME.service')

How to set-up

  1. Replace YOUR_VPN_SERVER_PRIVATE_IP with your OpenVPN gateway IP - this IP is permanently pinged and if it goes down openvpn process is restarted
  2. Replace YOUR_CONFIG_NAME with name of your openvpn client config in /etc/openvpn, e.g. /etc/openvpn/client.conf means CLIENT as YOUR_CONFIG_NAME