Monitor linux openvpn daemon and restart if disconnected
|
Posts: 403
Threads: 26
Joined: Aug 2015
Reputation:
39
[Not Solved]
Aug 14, 2015, 03:59 AM
(This post was last modified: Aug 17, 2015, 07:55 AM by Desgyz.)
If anyone is interested in the script.
Monitor linux openvpn daemon and restart if disconnected
1. Create file in location /etc/openvpn:
Code:
sudo nano /etc/openvpn/checkopenvpn.sh
2. Add the bash script:
Code:
#!/bin/bash
ps -ef | grep -v grep | grep openvpn
if [ $? -eq 1 ] ; then
/usr/sbin/openvpn --config username.ovpn --daemon --script-security 3 system
fi
Thanks to.
http://support.vpnsecure.me/articles/fre...sconnected
[save and exit]
Location of the file:
Code:
/etc/openvpn/checkopenvpn.sh
3. Make the file executable:
Code:
sudo chmod +x /etc/openvpn/checkopenvpn.sh
4.(Make sure you have crontabs package installed.)
5. Add this line below the # tag att the bottom: (it will run the script every hour)
Code:
0 * * * * /etc/openvpn/checkopenvpn.sh
[save and exit]
Info
*/5 * * * * /path/to/script - (it will run the script every 5 min)
*/30 * * * * /path/to/script - (it will run the script every 30 min)
Crontab logs its actions to Syslog:
Code:
sudo grep CRON /var/log/syslog
Fix this error message in the syslog (No MTA installed, discarding output)
Code:
sudo apt-get install postfix
Install it for local use only, I named the server raspberrypi
Big thanks to Desgyz and Mike who helped med with creating it.
Please feel free to post if you find any errors or modifications which would improve the script.
Posts: 79
Threads: 9
Joined: Aug 2015
Reputation:
2
[Not Solved]
Aug 16, 2015, 11:03 PM
Awesome idea! Would that work with Banana Pi?
Posts: 403
Threads: 26
Joined: Aug 2015
Reputation:
39
[Not Solved]
Aug 17, 2015, 03:02 AM
if you run Linux Debian on it, it would.
Skickat från min SM-G925F via Tapatalk
Posts: 21
Threads: 1
Joined: Jul 2015
Reputation:
6
[Not Solved]
Aug 17, 2015, 07:57 AM
It should also work on Ubuntu distros without too much tweaking
Sent from my iPhone using Tapatalk
Posts: 38
Threads: 11
Joined: Jul 2015
Reputation:
3
[Not Solved]
Aug 17, 2015, 06:07 PM
I could use this. Guess this is my project for later today.
Posts: 403
Threads: 26
Joined: Aug 2015
Reputation:
39
[Not Solved]
Nov 15, 2015, 05:33 PM
I found this script, can I use it on the rpi?
I think I have to modify it, any suggestions?
Code:
#cat vpn.sh
IP=192.168.0.1
RESULT=`ping -c 8 -W 2 $IP | grep transm | awk '{print $4}'`
echo "Result is $RESULT"
if [ $RESULT -eq 0 ]
then
/usr/sbin/openvpn --config /etc/openvpn/vpn.conf > /var/log/vpn.log&
echo "OpenVPN restart"
date
else
echo "No restart needed"
date
fi
Posts: 1,646
Threads: 2
Joined: Aug 2015
Reputation:
42
[Not Solved]
Nov 15, 2015, 09:44 PM
(This post was last modified: Nov 15, 2015, 09:44 PM by Mike.)
That script looks like it pings an IP and greps for 'transm' the transmission line and prints the 4th field which is the number before received
Code:
8 packets transmitted, 0 received, 100% packet loss, time 7006ms
My guess is the script is supposed to ping the VPN gateway which you'd need to find out yourself and if it fails restarts the OpenVPN. However, you have said before that when your VPN connection goes down then you can't access your Pi so maybe it needs to be the gateway of your router. Test pinging the router IP with the VPN connection up to see if you can (also ping it without the VPN to make sure icmp packets aren't blocked on the router).
Change IP to the gateway and then the openvpn line to the one you were using before
Code:
/usr/sbin/openvpn --config username.ovpn --daemon --script-security 3 system
Posts: 403
Threads: 26
Joined: Aug 2015
Reputation:
39
[Not Solved]
Nov 15, 2015, 10:57 PM
(This post was last modified: Nov 15, 2015, 10:58 PM by manne.)
Mike, I can access my PI's internally. But I can't access external network (Internet) from the PI. I can SSH into the PI and ping local networks but no internet access. If I reboot the PI everything is good again.
My Pi's are working good now. But I'm trying to find a script that restarts the network or the VPN. The script we did at first (the one this post is about is if the openvpn client is disconnected)
Or am I thinking this wrong?
My routes IP is 192.168.0.1.
Posts: 1,646
Threads: 2
Joined: Aug 2015
Reputation:
42
[Not Solved]
Nov 15, 2015, 11:04 PM
(Nov 15, 2015, 10:57 PM)manne Wrote: Mike, I can access my PI's internally. But I can't access external network (Internet) from the PI. I can SSH into the PI and ping local networks but no internet access. If I reboot the PI everything is good again.
This happens when? If the VPN goes down you can still access the Pi internally but the Pi has no internet access?
This sounds like OpenVPN's routes not being flushed if it loses connection, my guess is restarting the service should restore the routes. Alternatively you can add another default route in another routing table ( see here)
If it is the OpenVPN connection then restarting the service and re-establishing connection should fix it and you can do that by combining the two scripts in this thread. Your original one restarts the service and the second you found re-establishes connection to the VPN.
Posts: 403
Threads: 26
Joined: Aug 2015
Reputation:
39
[Not Solved]
Nov 15, 2015, 11:11 PM
(Nov 15, 2015, 11:04 PM)Mike Wrote: This happens when? If the VPN goes down you can still access the Pi internally but the Pi has no internet access?
Correct.. I'm just waiting for it to happen again just to verify it.
|
|
Recent Posts
|
About Swap
jonescelinaa Apr 10, 2024, 06:58 AM
|
Tracker Status: Error Connection Time Out
jonesPhedra Apr 04, 2024, 08:17 AM
|
Split Tunnel Docker Containers
jonesPhedra Mar 27, 2024, 03:10 AM
|
Plex server not powerful enough, but only with s...
jonesPhedra Mar 27, 2024, 03:02 AM
|
game Geometry Dash Scratch
jonescelinaa Jan 31, 2024, 04:21 AM
|
Latest unread posts | Unanswered posts |
|