OpenVPN Linux auto-restart

From EnigmaGuy Wiki
Revision as of 00:13, 6 January 2019 by Enigmaguy (talk | contribs) (Created page with "== Intro == There are basically two files to install on linux * openvpn-restart.service * restart_openvpn.py You can get these files from [https://github.com/koss822/misc/tre...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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