Skip to content

Instantly share code, notes, and snippets.

@hannesbe
Last active January 4, 2024 13:53
Show Gist options
  • Save hannesbe/03020b0f9662a9e973f7cceadcdf0208 to your computer and use it in GitHub Desktop.
Save hannesbe/03020b0f9662a9e973f7cceadcdf0208 to your computer and use it in GitHub Desktop.
Script to keep VPN alive on Synology DSM. Checks if IP is pingable and if not: disconnect VPN, reconnect VPN & add routes
# syno-vpn-keepalive.sh
# ---
# Script to keep VPN alive on Synology DSM.
# Checks if IP is pingable and if not:
# disconnect VPN, reconnect VPN & add routes
# ---
# Modify vars:
# - CHECKIP: IP to check to be pingable before reconnecting VPN
# - NAME (Synology VPN name),
# - ID (Synology VPN ID), SSH to NAS & run this to find ID
# sudo grep conf_id /usr/syno/etc/synovpnclient/vpnc_last_connect
# - PROTO (pptp|l2tp|openvpn),
# - DEV
# VPN network device -
# check using ifconfig while VPN connected, *ppp200* for example)
# - ROUTE1,
# Route to add after connecting VPN (network/prefix)
# - ROUTE2
# Another route to add efter connecting VPN (network/prefix)
#
# To install, go to **Control Panel > Task Scheduler**
#
# Hit **Create > Scheduled Task > User-defined Script**
#
# ## General
# - Task: **VPN keepalive**
# - User: **root**
#
# ## Schedule
# - Run on the following days: **Daily**
# - First run: **00:00**,
# - Frequency: **Every 5 min(s)**,
# - last run time: **23:55**
#
# ## Task settings
# - Run command - User defined script:
# *paste this entire script's content*
# ---
CHECKIP='10.1.10.48'
NAME='Connexeon'
ID='p1489396766'
PROTO='pptp'
DEV='ppp200'
ROUTE1='10.0.0.0/12'
ROUTE2='10.111.0.0/24'
if ping -c 1 $CHECKIP &> /dev/null
then
echo "VPN is running ($CHECKIP pingable)"
synovpnc get_conn
route
else
echo "Reconnecting VPN ($CHECKIP unresponsive)"
echo "Killing VPN ($NAME)"
synovpnc kill_client --name=$NAME
echo "Reconnecting VPN ($NAME - $PROTO)"
echo conf_id=$ID > /usr/syno/etc/synovpnclient/vpnc_connecting
echo conf_name=$NAME >> /usr/syno/etc/synovpnclient/vpnc_connecting
echo proto=$PROTO >> /usr/syno/etc/synovpnclient/vpnc_connecting
synovpnc reconnect --protocol=$PROTO --name=$NAME
echo "Adding route ($ROUTE1 - $DEV)"
route add -net $ROUTE1 dev $DEV
echo "Adding route ($ROUTE2 - $DEV)"
route add -net $ROUTE2 dev $DEV
fi
exit 0
@jfberry
Copy link

jfberry commented Mar 24, 2022

I also upgraded a DSM6 using this in the field to DSM7 and it kept on trucking

@bc-teixeira
Copy link

It worked for me like a charm! Thanks a lot!

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