Hi Alzhao.
Can you kindly explain what the following commands in this script do:
continue # surely the script continues without this line?
vpn_pid=$(pidof openvpn) # what is pidof openvpn?
tun0_ifname=$(ifconfig tun0) #checking for value 0/1 to see if VPN is dis/en abled?
if [ -z “$tun0_ifname” ] && [ -z “$vpn_pid” ] # what does the z do and what does the whole line do in layman’s terms?
Thanks,
G
EDIT:
OK, I worked it out myself.
continue > simply continues the loop
the “-z” check to see if the value is zero. If either value is zero (vpn_pid or tun0_ifname) then it restarts the VPN (/etc/init.d/startvpn restart).
Hi, I use VPN unlimited and it seems to drop my vpn connection if it’s not being used. I used the vpn_reconnect script and it works great at keeping the connection alive. The problem I have is getting that script to run every time the device starts. I added it to rc.local but it does not seem to run on startup. I have made it and rc.local both executable. Any ideas ?
Did not work for me with Private internet access with AR300M on 2.27 firmware.
I had openvpn on and working, but then after what I believe was a reconnect openvpn said it was up and connected but I could not access any websites, it was behaving like it does when openvpn disconnects and “Force VPN” box is checked. I will try again without that Force VPN box checked, although I’d prefer to have it on.
Edit: I tried again with the Force VPN box unchecked. OpenVPN page on 192.168.8.1 GUI shows VPN is connected, but in verifying it really is not (checking IP address online shows this). I can access the internet but it is not really through VPN and my local IP is exposed. So I don’t know what the problem is, but reconnect script is not really working for me for some reason.
Maybe I don’t understand - I am not sure PIA aka Private Internet Access ever sends an EXIT command, the VPN connection just fails and needs to be reconnected, which I’d hope this script would do because it fails quite frequently, multiple times a day for the AR300M on PIA (vs once every few weeks on Linux Mint using OpenVPN as I do not like the Linux PIA app).
As I noted it did seem to reconnect or at least claimed OpenVPN was connected, but it really wasn’t as my local IP addr was in use…which makes using the reconnect script worse than not using it because it fools me into believing I am still connected and exposes my private information. If the reconnect script needs a clean “EXIT” from the openvpn server to work, I guess it will not solve the vast majority of VPN drops that I see on AR300M.
Hi alzhao, many thanks for replying.
I did have the wrong path and it now does run on startup.
The issue I now face is that if the openvpn client get stuck in “connecting” mode it does not appear that the vpn_reconnect kills it and tries again.
I have to still login to the openvpn client page and click the apply button till I get it connected.
The message I was getting was
OpenVpn is connecting …
Last log WARNING: this configuration may cache passwords in memory – use the auth-nocache option to prevent this
Initialization Sequence Completed
[openvpn2.vpnunlimitedapp.com] Inactivity timeout (–ping-exit), exiting
/usr/bin/ip addr del dev tun0 local 10.200.0.234 peer 10.200.0.233
SIGTERM[soft,ping-exit] received, process exiting
vpn_reconnect doesn’t seem to detect that’s not connected, kill it and try again which I think is what I need.
It’s what I’m currently thinking of trying. A combination of scripts from this thread…
#!/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 glconfig.openvpn.enable)
if [ “$vpn_enabled” != “1” ];then
echo “VPN not enabled, check 20 seconds later”
sleep 20
continue
fi
if ! ping -I tun0 -c5 -w6 lede-project.org; then
echo “VPN enabled but not running, restarting it”
/etc/init.d/startvpn restart
else
echo “VPN is connected and connecting, check 20 seconds later”
fi
#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 glconfig.openvpn.enable)
if [ “$vpn_enabled” != “1” ]; then
echo “VPN not enabled, check 20 seconds later”
sleep 20
continue
fi
if [ -z “$tun0_ifname” ] && [ -z “$vpn_pid” ]; then
echo “VPN enabled but not running, restarting it”
/etc/init.d/startvpn restart
else
echo “VPN is connected and connecting, check 20 seconds later”
fi
sleep 20
done
Step 3 and the last step try to execute
Using Putty because it is the only program that can execute command
And kindly please show me if my step is right or wrong. I’m trying to sum it up right now
I hope this will help. I had to work this out myself as there seems to be very little newbie help in here.
If I have made any mistakes please feel free to correct me.
This seems to have worked. The only issue left seems to be that occasionally the port forwarding stops working even though the vpn reconnects.
Download install and run PuTTY
Enter Host Name or IP address. (this will usually be 192.168.8.1)
Click open.
Login as root
Password will be whatever you set up when you first configured the router.
Now type in the following apart from the stuff in brackets as I have added these to explain what each line does to make it easier to follow:
opkg install sudo --force-depends (installs sudo to allow full control)
sudo touch /usr/bin/vpn_reconnect (create the document)
sudo vi /usr/bin/vpn_reconnect (edit the document)
(paste in the script)
(press esc)
:w (save document)
:q (exit the editor)
sudo chmod +x /usr/bin/vpn_reconnect (make script execute only)
sudo vi /etc/rc.local (edit the rc.local document)
(scroll down to the botton of document and add this:)
/usr/bin/vpn_reconnect &
Will this work today with AR300M and MT300N-V2 Mango Routers ? I have same Problem that with Openvpn config from Airvpn after my Network gets New IP even if internet is available 20 sec later the Openvpn is disconnected and won’t reconnect again. In my Openvpn config i tryed with keepalive settings but that does not help. I tryed with keepalive 10 60 and 10 120 but it won’t reconnect. Only way i have to click on Abort and reconnect in OpenVPN Tab. Is there any Option or Autoscript i can Install with luci or any other way ? Is this Procedure in this Thread the same today ?
I tryed to install the script but as soon i enter first command i get error
Unknown package ‘sudo’.
Collected errors:
opkg_install_cmd: Cannot install package sudo.
There must be an easy way of reconnecting the vpn with mango automaticly after IP change or network outtage ???
I was able to install the script on 2 devices. First before installing Sudo it was neccessary to start with opkg update. After That i was able to use ur guide and install Sudo. But unfortunaly it is not working. When i get new IP VPN Still brakes and does not reconnect with openvpn. How can i check if the script is working ? After i have instalelled the script i have rebooted the Mango just to be sure it will work.
This is the Script Text i have inserted:
#!/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 glconfig.openvpn.enable)
if [ “$vpn_enabled” != “1” ]; then
echo “VPN not enabled, check 20 seconds later”
sleep 20
continue
fi
if [ -z “$tun0_ifname” ] && [ -z “$vpn_pid” ]; then
echo “VPN enabled but not running, restarting it”
/etc/init.d/startvpn restart
else
echo “VPN is connected and connecting, check 20 seconds later”
fi
sleep 20
done
And than finally in the rc.local i have placed this Line at the End and saved it
/usr/bin/vpn_reconnect &
And i have used all the other Steps as mentioned like sudo chmod +x /usr/bin/vpn_reconnect and so on.