Update: The script is now being maintained on my GitHub account. If you experience any issues, please feel free to open an issue.
Synology's DiskStation Manager (DSM) allows a DiskStation or RackStation to connect to a remote private network over the Internet via a VPN connection. While creating a VPN profile in DSM, there is an option that allows DSM to Reconnect when the VPN connection is lost. Unfortunately, as a simple Google search will reveal, this option does not work reliably, if at all.
There is a thread on the Synology Community Forum on which users have shared scripts they use to programmatically reconnect failed VPN connections. These scripts, however, have some limitations in my environment, so I decided to write my own.
There were a number of goals in writing this script:
- Require no script modifications before putting into production
- Work with all VPN protocols (L2TP, PPTP, and OpenVPN)
- Run natively from DSM without needing backend console access
- Send email notifications when encountering warning or error conditions
Please note that this script was tested on DSM 6.0.2, but from what I have seen, it will probably work on DSM 5.0 and up. Finally, please use this script at your own risk.
First, you will need to create a VPN profile in DSM. Synology has a KB article on this, so I'm not going to cover it here. I will mention, however, that this script obeys the Reconnect when the VPN connection is lost option, so make sure it is enabled. Additionally, this script only works if there is a single VPN connection on the system at a time.
Second, you will need to set up email notifications in DSM. Again, Synology has a KB article on this. Once the email notification settings have been applied, click the Send a test email button to confirm the settings. Otherwise, it will be difficult to troubleshoot issues.
We will be generally following a Synology KB article to install the script as a scheduled task in DSM.
Open the Control Panel and navigate to System > Task Scheduler. Under Create > Scheduled Task, click User-defined script. This will open up a Create task window.
On the General tab, name the task something like Reconnect VPN for easy identification. Make sure the task is running as root and that the Enabled box is selected.
On the Schedule tab, make sure the Date option is set to Run on the following days: Daily. To schedule the script to run every 5 minutes, set the Frequency to Every 5 minute(s). Note that by default, the script will only run for the first 55 minutes of the day (00:00 to 00:55). To ensure the script will run every 5 minutes for the entire day, change the Last run time to 23:55.
On the Task Settings tab, enable Send run details by email and enter your email address in the Email box. I would recommend enabling the Send run details only when the script terminates abnormally option. The script was written using different exit codes, allowing it to work well with this option. If you do not enable this option, you will receive an email every time the script runs, whether there is an error or not.
On the bottom of the Task Settings tab, paste the entire script into the User-defined script box.
Finally, click the OK button to close the Create task window and then click the Save button to save the changes to the Task Scheduler.
Hopefully, Synology will eventually fix this broken feature in DSM, but until then, you can use this script to enjoy more a stable VPN connection without the need to poke around or make changes on the backend of your Synology NAS.