Last active
September 19, 2024 10:59
-
-
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
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
# 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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It worked for me like a charm! Thanks a lot!