Reconnect-Script for staying online every time! Based on GL.iNet Routers
If you don’t want to use a cronjob or the cronjob isn’t working with your vpn-provider, you can use that script to let your router listening and reconnect if necessary.
-> You can use the ovpn-file from the provider without tweaking (tweaking is recommended with this script too), your router will check if the connection is lost and reconnect automatically if there is no connection.
Let’s start:
-
If you are a new vpn-user, you can check my tutorial how to connect to your vpn provider with ovpn-file: HowToOpenVPNClient
-
Login in Luci and than go to: System->Software and search for nano and install it
-
Go into Terminal (Linux) and write:
ssh root@192.168.1.1
->hit enter and use your forris/luci passwort
- Write this and hit enter:
nano /usr/bin/vpn_reconnect
Copy/Paste this script and change for your VPN-Connection:
“PublicVPN_Fr_Paris” in the script is the Name of my created VPN
(in Luci->Services->OpenVPN and the name of your created VPN you sould change in the script below)
#!/bin/sh
#wait for the openvpn to connect for the first time
sleep 120
while [ true ]; do
#check if openvpn is enabled, if not, go to next loop
vpn_enabled=$(uci get /etc/config/openvpn.PublicVPN_Fr_Paris.enabled)
if [ "$vpn_enabled" != "1" ]; then
echo "VPN not enabled, check 20 seconds later"
sleep 20
continue
fi
vpn_pid=$(pidof openvpn)
tun0_ifname=$(ifconfig tun0)
if [ -z "$tun0_ifname" ] && [ -z "$vpn_pid" ]; then
echo "VPN enabled but not running, restarting it"
/etc/init.d/openvpn restart
else
echo "VPN is connected and connecting, check 20 seconds later"
fi
sleep 20
done
and then do Strg + o than Enter than Strg + x
Then:
chmod +x /usr/bin/vpn_reconnect
Then open the rc.local file:
nano /etc/rc.local
Add this to the end of the file just before exit:
/usr/bin/vpn_reconnect &
Do again Strg + o, Enter, Strg + x to save and quit again.
The script should run on boot now
Script with manual time to set how often the connection will be checked
This script should: a) keeps the connection up (restarts it) and b) at no point allows non-VPN traffic through IF that’s how you’ve set things up. Also you can set the time by yourself how often there will be a connnection check
-
Login in Luci and than go to: System->Software and search for nano and install it
-
Go into Terminal (Linux) and write:
ssh root@192.168.1.1
->hit enter and use your forris/luci passwort
- Write this and hit enter:
nano /usr/bin/vpn_restart
Copy/Paste this script and edit this lines:
“PublicVPN_Fr_Paris” in the script is the Name of my created VPN
(in Luci->Services->OpenVPN and the name of your created VPN you sould change in the script below)
#!/bin/sh
# Should openvpn already be in operation? If not, nothing to do, exit.
enabled=$(uci get /etc/config/openvpn.PublicVPN_Fr_Paris.enabled)
vpn_client=$(uci get /etc/config/network.VPN) # removed when startvpn stopped explicitly
if [ "$enabled" != "1" ] || [ "$vpn_client" != "interface" ]; then
exit 0
fi
# First hop should be to the internal VPN gateway (10.8.8.1) if VPN up.
# If we're going through VPN then all is well, do nothing.
first_hop=$(traceroute 8.8.8.8 2>&1 | head -2 | tail -1 | awk '{print $2}')
if [ "$first_hop" == "10.8.8.1" ]; then
logger -t VPN_restart VPN is fine.
exit 0
fi
killall openvpn 2>/dev/null
ovpn=$(uci get /etc/config/openvpn.PublicVPN_Fr_Paris.enabled)
/usr/sbin/openvpn "$ovpn" &
(sleep 1; /etc/init.d/network reload) &
logger -t VPN_restart VPN was down and had to be restarted.
-> Hit “enter” and write ":wq
- To set up a cronjob, write:
crontab -e
and entered:
*/5 * * * * /usr/bin/vpn_restart
which is the name and location of my script.
- A few other commands, which may or may not be necessary, to ensure that cron was running:
/etc/init.d/cron start
/etc/init.d/cron enable
/etc/init.d/cron restart
PS:
You can actually interact with cron through the web panel as well: Advanced settings > System > Scheduled tasks.
As for the “-t” option, it just adds a “tag” to the log so that logs reference the tag as opposed to user.root or whatever the default is. (There is a main page out there but I can’t find it right now!)