Skip to content

Instantly share code, notes, and snippets.

@GamerGun
Last active Jun 7, 2020
Embed
What would you like to do?
#!/usr/bin/env bash
#===============================================================================
# FILE: ReconnectSynologyVPN.sh
#
# DESCRIPTION: Reconnect a disconnected VPN session on Synology DSM
# SOURCE(S): https://forum.synology.com/enu/viewtopic.php?f=241&t=65444
#
# AUTHOR: Ian Harrier
# VERSION: 1.0.0
# LICENSE: MIT License
#===============================================================================
#-------------------------------------------------------------------------------
# Pull in VPN config files
#-------------------------------------------------------------------------------
if [[ -f /usr/syno/etc/synovpnclient/l2tp/l2tpclient.conf ]]; then
L2TP_CONFIG=$(cat /usr/syno/etc/synovpnclient/l2tp/l2tpclient.conf)
else
L2TP_CONFIG=""
fi
if [[ -f /usr/syno/etc/synovpnclient/openvpn/ovpnclient.conf ]]; then
OPENVPN_CONFIG=$(cat /usr/syno/etc/synovpnclient/openvpn/ovpnclient.conf)
else
OPENVPN_CONFIG=""
fi
if [[ -f /usr/syno/etc/synovpnclient/pptp/pptpclient.conf ]]; then
PPTP_CONFIG=$(cat /usr/syno/etc/synovpnclient/pptp/pptpclient.conf)
else
PPTP_CONFIG=""
fi
#-------------------------------------------------------------------------------
# Process config files
#-------------------------------------------------------------------------------
# Concatenate the config files
CONFIGS_ALL="$L2TP_CONFIG $OPENVPN_CONFIG $PPTP_CONFIG"
# How many VPN profiles are there?
CONFIGS_QTY=$(echo "$CONFIGS_ALL" | grep -e '\[l' -e '\[o' -e '\[p' | wc -l)
# Only proceed if there is 1 VPN profile
if [[ $CONFIGS_QTY -eq 1 ]]; then
##/usr/syno/bin/synologset1 sys info 0x11100000 "check-vpn: There is 1 VPN profile. Continuing..."
echo "[I] There is 1 VPN profile. Continuing..."
elif [[ $CONFIGS_QTY -gt 1 ]]; then
/usr/syno/bin/synologset1 sys err 0x11100000 "check-vpn: There are $CONFIGS_QTY VPN profiles. This script supports only 1 VPN profile. Exiting..."
echo "[E] There are $CONFIGS_QTY VPN profiles. This script supports only 1 VPN profile. Exiting..."
exit 1
else
/usr/syno/bin/synologset1 sys warn 0x11100000 "check-vpn: There are 0 VPN profiles. Please create a VPN profile. Exiting..."
echo "[W] There are 0 VPN profiles. Please create a VPN profile. Exiting..."
exit 1
fi
#-------------------------------------------------------------------------------
# Set variables
#-------------------------------------------------------------------------------
PROFILE_ID=$(echo $CONFIGS_ALL | cut -d "[" -f2 | cut -d "]" -f1)
PROFILE_NAME=$(echo "$CONFIGS_ALL" | grep -oP "conf_name=+\K\w+")
PROFILE_RECONNECT=$(echo "$CONFIGS_ALL" | grep -oP "reconnect=+\K\w+")
if [[ $(echo "$CONFIGS_ALL" | grep '\[l') ]]; then
PROFILE_PROTOCOL="l2tp"
elif [[ $(echo "$CONFIGS_ALL" | grep '\[o') ]]; then
PROFILE_PROTOCOL="openvpn"
elif [[ $(echo "$CONFIGS_ALL" | grep '\[p') ]]; then
PROFILE_PROTOCOL="pptp"
fi
#-------------------------------------------------------------------------------
# Check the VPN connection
#-------------------------------------------------------------------------------
if [[ $(/usr/syno/bin/synovpnc get_conn | grep Uptime) ]] && [[ $(ping -c 1 google.com) ]]; then
/usr/syno/bin/synologset1 sys info 0x11100000 "check-vpn: VPN is already connected and retrieving data. Exiting..."
echo "[I] VPN is already connected and retrieving data. Exiting..."
exit 0
fi
if [[ $PROFILE_RECONNECT != "yes" ]]; then
/usr/syno/bin/synologset1 sys warn 0x11100000 "check-vpn: VPN is not connected, but reconnect is disabled. Please enable reconnect for for the \"$PROFILE_NAME\" VPN profile. Exiting..."
echo "[W] VPN is not connected, but reconnect is disabled. Please enable reconnect for for the \"$PROFILE_NAME\" VPN profile. Exiting..."
exit 1
else
/usr/syno/bin/synologset1 sys warn 0x11100000 "check-vpn: VPN is not connected or is not retrieving data. Attempting to reconnect..."
echo "[W] VPN is not connected or is not retrieving data. Attempting to reconnect..."
/var/packages/DownloadStation/scripts/start-stop-status stop
fi
#-------------------------------------------------------------------------------
# Reconnect the VPN connection
#-------------------------------------------------------------------------------
/usr/syno/bin/synovpnc kill_client --name=$PROFILE_NAME
sleep 20
echo conf_id=$PROFILE_ID > /usr/syno/etc/synovpnclient/vpnc_connecting
echo conf_name=$PROFILE_NAME >> /usr/syno/etc/synovpnclient/vpnc_connecting
echo proto=$PROFILE_PROTOCOL >> /usr/syno/etc/synovpnclient/vpnc_connecting
#/usr/syno/bin/synovpnc reconnect --protocol=$PROFILE_PROTOCOL --name=$PROFILE_NAME --retry=1 --interval=30
/usr/syno/bin/synovpnc connect --id=$PROFILE_ID
sleep 20
#-------------------------------------------------------------------------------
# Re-check the VPN connection
#-------------------------------------------------------------------------------
if [[ $(/usr/syno/bin/synovpnc get_conn | grep Uptime) ]] && [[ $(ping -c 1 google.com) ]]; then
/usr/syno/bin/synologset1 sys info 0x11100000 "check-vpn: VPN successfully reconnected. Exiting..."
echo "[I] VPN successfully reconnected. Exiting..."
/volume2/homes/tommy/Scripts/port_forwarding.sh
##The file above can be found here (remove if not required); https://gist.github.com/GamerGun/20f08da8ff2712db6e62c5edc83fd541
exit 1
else
/usr/syno/bin/synologset1 sys err 0x11100000 "check-vpn: VPN failed to reconnect. Exiting..."
echo "[E] VPN failed to reconnect. Exiting..."
exit 1
fi
@GamerGun
Copy link
Author

GamerGun commented Jul 3, 2018

Sorry for the multiple revisions, i messed up spaces when trying to fix an issue where reconnect is not always working.
Also, take note of line 116!

@clobber88
Copy link

clobber88 commented Jun 7, 2020

Really nice script. I had to modify it a little for the Synology router because it only has the ash (not bash) shell. For example, it does not have the grep -P flag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment