-
-
Save hannesbe/03020b0f9662a9e973f7cceadcdf0208 to your computer and use it in GitHub Desktop.
| # 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 | |
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.
Dear hannesbe,
Thank you and this helps.
Awesome :-) Just what I needed and should be in standard DSM!
Thanks for sharing!
You are the real MVP! Thank you <3
This is super awesome, ... has anyone tried this on DSM7 before I update?
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!
I also upgraded a DSM6 using this in the field to DSM7 and it kept on trucking
It worked for me like a charm! Thanks a lot!
Dear hannesbe,
How do I find out ROUTE1 and ROUTE2? Appreciate your response.