Last active
June 5, 2017 16:29
-
-
Save ajin/f8092cc81c23e7aeec635928e0628793 to your computer and use it in GitHub Desktop.
Reconnecting a failed VPN connection on Synology by Ian Harrier
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
#!/bin/bash | |
## Reconnect Synology VPN | |
## Written by Ian Harrier | |
## Based on https://forum.synology.com/enu/viewtopic.php?f=241&t=65444 | |
## From: https://blog.harrier.us/reconnecting-a-failed-vpn-connection-on-synology-dsm-6/ | |
### | |
# Process config files | |
### | |
# Pull in the VPN config files | |
L2TP_CONFIG=$(cat /usr/syno/etc/synovpnclient/l2tp/l2tpclient.conf) | |
OPENVPN_CONFIG=$(cat /usr/syno/etc/synovpnclient/openvpn/ovpnclient.conf) | |
PPTP_CONFIG=$(cat /usr/syno/etc/synovpnclient/pptp/pptpclient.conf) | |
# 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 | |
echo "INFO: There is 1 VPN profile. Continuing..." | |
elif [ $CONFIGS_QTY -gt 1 ] | |
then | |
echo "ERROR: There are $CONFIGS_QTY VPN profiles. This script supports only 1 VPN profile. Exiting..." | |
exit 1 | |
else | |
echo "WARNING: 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 -q "Uptime" | |
then | |
echo "INFO: VPN is already connected. Exiting..." | |
exit 0 | |
fi | |
if [ $PROFILE_RECONNECT != "yes" ] | |
then | |
echo "WARNING: VPN is not connected, but reconnect is disabled. Please enable reconnect for for the \"$PROFILE_NAME\" VPN profile. Exiting..." | |
exit 1 | |
else | |
echo "WARNING: VPN is not connected. Attempting to reconnect..." | |
fi | |
### | |
# Reconnect the VPN connection | |
### | |
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 | |
### | |
# Re-check the VPN connection | |
### | |
if /usr/syno/bin/synovpnc get_conn | grep -q "Uptime" | |
then | |
echo "INFO: VPN successfully reconnected. Exiting..." | |
exit 1 | |
else | |
echo "ERROR: 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