Skip to content

Instantly share code, notes, and snippets.

@hannesbe
Last active September 19, 2024 10:59
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
@aelharda
Copy link

aelharda commented Dec 5, 2019

Hello, where do i get the information for ROUTE1 & ROUTE2 ? Thank you.

@guptash1
Copy link

Dear hannesbe,

How do I find out ROUTE1 and ROUTE2? Appreciate your response.

@hannesbe
Copy link
Author

hannesbe commented Feb 5, 2021

How do I find out ROUTE1 and ROUTE2? Appreciate your response.

These are your VPN subnets in CIDR notation. This example adds routes for 2 different VPN subnets. You may remove lines 46, 69 and 70 if you only have one VPN subnet.

@guptash1
Copy link

guptash1 commented Feb 6, 2021

Dear hannesbe,

Thank you and this helps.

@EJK64
Copy link

EJK64 commented May 27, 2021

Awesome :-) Just what I needed and should be in standard DSM!
Thanks for sharing!

@Azumi0
Copy link

Azumi0 commented Jun 26, 2021

You are the real MVP! Thank you <3

@jfberry
Copy link

jfberry commented Jul 1, 2021

This is super awesome, ... has anyone tried this on DSM7 before I update?

@Winblackka
Copy link

This is super awesome, ... has anyone tried this on DSM7 before I update?

Yes, it is working!

Is it possible to integrate an email notification if the connection isnt working?

Thank you!

@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