This is just part of old tutorial. It still works same as new tutorial above, I just wanted to simplify it to people so they don't have to use Putty, vi, edit crontab etc.
5. Auto reconnection when VPN is down.
Since when you made your VPN connection on your Synology, you checked "Reconnect" option, Syno will try to reconnect automaticly when connection fails.
But in some cases, your network will be offline long enough and Syno will stop trying to reconnect, or will hang with VPN connection established, but not working.
In those cases you can use this auto reconnect script.
This is reconnect script. Save it in file named whatever you want. I'm using file name "synovpn_reconnect". All instructions how to use it are inside script in comments, but I will repeat them in this post also.
#VPN Check script modified Sep 11, 2016
#Script checks if VPN is up, and if it is, it checks if it's working or not. It provides details like VPN is up since, data #received/sent, VPN IP & WAN IP.
#If VPN is not up it will report it in the log file and start it
#Change LogFile path to your own location.
#Save this script to file of your choosing (for example "synovpn_reconnect"). Store it in one of your Synology shared folders and chmod it: "chmod +x /volume1/shared_folder_name/your_path/synovpn_reconnect"
#Edit "/etc/crontab" and add this line without quotes for starting script every 10 minutes: "*/10 * * * * root /volume1/shared_folder_name/your_path/synovpn_reconnect"
#After that restart cron with: "/usr/syno/sbin/synoservicectl --restart crond"
#!/bin/sh
DATE=$(date +"%F")
TIME=$(date +"%T")
VPNID=$(grep "\[.*\]" /usr/syno/etc/synovpnclient/openvpn/ovpnclient.conf | cut -f 2 -d "[" | cut -f 1 -d "]")
VPNNAME=$(grep conf_name /usr/syno/etc/synovpnclient/openvpn/ovpnclient.conf | cut -f 2 -d "=")
LogFile="/volume1/filmovi/Backup/airvpn/check_airvpn_$DATE.log"
PUBIP=$(curl -s -m 5 icanhazip.com)
#PUBIP=$(curl -s -m 5 ipinfo.io/ip)
#PUBIP=$(curl -s -m 5 ifconfig.me)
CHECKIP=$(echo $PUBIP | grep -c ".")
start_vpn()
{
echo "VPN is down. Attempting to (re)start now." >> $LogFile
# /usr/syno/bin/synovpnc kill_client --protocol=openvpn --name=$VPNNAME
/usr/syno/bin/synovpnc kill_client
/bin/kill `cat /var/run/ovpn_client.pid` 2>/dev/null
sleep 35
echo 1 > /usr/syno/etc/synovpnclient/vpnc_connecting
echo conf_id=$VPNID > /usr/syno/etc/synovpnclient/vpnc_connecting
echo conf_name=$VPNNAME >> /usr/syno/etc/synovpnclient/vpnc_connecting
echo proto=openvpn >> /usr/syno/etc/synovpnclient/vpnc_connecting
/usr/syno/bin/synovpnc reconnect --protocol=openvpn --name=$VPNNAME >> $LogFile
}
sleep 6
echo "======================================" >> $LogFile
echo "$DATE $TIME" >> $LogFile
if ifconfig tun0 | grep -q "00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00"
then
if [ "$CHECKIP" == 1 ]
then
IPADDR=$(/sbin/ifconfig tun0 | grep 'inet addr' | cut -d: -f2 | awk '{print $1}')
RXDATA=$(/sbin/ifconfig tun0 | grep "bytes:" | cut -d: -f2 | awk '{print $1,$2,$3}')
TXDATA=$(/sbin/ifconfig tun0 | grep "bytes:" | cut -d: -f3 | awk '{print $1,$2,$3}')
UPTIME=$(cat /var/log/messages | grep "$IPADDR" | awk '{print $1}' | tail -1)
UPTIME=$(date -d"$UPTIME" +"%Y/%m/%d %H:%M:%S")
echo "VPN is up since: $UPTIME" >> $LogFile
echo "Session Data RX: $RXDATA" >> $LogFile
echo "Session Data TX: $TXDATA" >> $LogFile
echo "VPN IP is: $IPADDR" >> $LogFile
echo "WAN IP is: $PUBIP" >> $LogFile
else
start_vpn
fi
else
start_vpn
fi
exit 0
(1) Enable SSH on your Synology if you didn't already.
- As admin go to "Control panel" - "Terminal & SNMP" (you need to enable advanced mode in top right corner of control panel for this)
- Check "Enable SSH service"
- Click "Apply"
(2) Save script above in file "synovpn_reconnect". Make sure to save it in UNIX UTF8, not windows. You can do that on windows with Notepad++, just open file with Notepad++, click "Encoding" - "Convert to UTF-8 without BOM" and them save file.
(3) Edit script variables so it works for your system. You only need to edit this part:
LogFile="/volume1/video/Backup/airvpn/check_airvpn_$DATE.log"
Thanks to foobar666, you no longer need to enter VPNID or VPNNAME, it will detect them automatically.
Now you only need to change your LogFile variable to match your wishes.
After you finish editing script, save it.
(4) Move or copy "synovpn_reconnect" to your Synology shared drive. Doesn't matter which, just be sure to know full path to it. If you only have 1 volume/drive, with multiple shared folders, your path should look similar to this:
/volume1/shared_folder_name/your_path/
So for example, if you keep your files in default CloudStation folder, your path should look something like this:
/volume1/home/your_username/CloudStation/
You can also do all this with VI, check original tutorial for that.
(5) Now use Putty if you are on windows, or your terminal on linux, to access your Synology via SSH. I will not tutor you how to do that, learn.
admin@192.168.1.100 or username@192.168.1.100 + password, or whatever your Syno LAN IP is.
(6) Now type this into Putty/terminal:
sudo chmod +x /volume1/shared_folder_name/your_path/synovpn_reconnect
You need to chmod it to be executable. You will notice I use "sudo". It's because my admin username isn't default "admin". If you are using default "admin" user, then you probably don't need sudo.
(7) Setup cron so it automatically starts your script every X minutes / hours / days.
To setup it enter this:
vi /etc/crontab
And then press "i" to enter editing mode. Go to last line, and start new line with this:
*/10 * * * * root /volume1/shared_folder_name/your_path/synovpn_reconnect
Note that those ARE NOT spaces, those are TABS. This will start your script every 10 minutes. Change to whatever you want.
Then press ESC key, and then type:
:wq
To exit VI and save file.
After that type:
/usr/syno/sbin/synoservicectl --restart crond
To restart cron (or restart your Synology).
Tip: If you don't want logfile, you can comment out those lines, or remove ">> $LogFile" code from whole script.