Last active
June 7, 2020 07:02
-
-
Save GamerGun/0c82142f434178867c1de0cbd29622a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.