OpenVPN Linux auto-restart

From EnigmaGuy Wiki
Jump to: navigation, 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

 1 #!/usr/bin/env python3
 2 import os,time
 3 def reachable():
 4     for i in range(0,3):
 5         time.sleep(60)
 6         if os.system('ping -c1 YOUR_VPN_SERVER_PRIVATE_IP 2>&1 > /dev/null') == 0:
 7             print('ping succesfull')
 8             return True
 9         print('host unreachable')
10     return False
11 
12 
13 
14 while(True):
15     if reachable():
16         continue
17     print('restarting openvpn')
18     os.system('systemctl restart [email protected]_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
Share your opinion