OpenVPN Linux auto-restart
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
- 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
- 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