Git Product home page Git Product logo

synology-scripts's Introduction

synology-scripts

Scripts for Synology DSM

reconnect-vpn.sh

This script can be used as a workaround for Synology DSM's less-than-ideal reconnect behavior when a VPN connection is lost. The actual magic in this script was originally written by users on a thread on the Synology Forum. This script just provides a user-friendly wrapper to their code. For more information, including installation instructions, refer to my blog post.

Version History

  • 1.1.0: Extra customization options are included at the beginning of the script. Feel free to customize these to your liking.
  • 1.2.0: The following exit codes are used:
    • 0: reconnect not needed
    • 1: reconnect successful
    • 2: reconnect failed
    • 3: configuration error
  • 1.3.0: An option is added to allow pinging a custom IP address or hostname to validate VPN connectivity.
  • 1.4.0: An option is added to choose a specific VPN profile to reconnect, if multiple profiles exist. In this configuration, you could run multiple instances of this script, each targeting a specific VPN profile.
  • 1.5.0: Options are added to run external scripts at various points. Note that the scripts must be executable, and if there are spaces in the script paths, you must either escape the spaces (e.g. NO_RECONNECT_SCRIPT=/volume1/Scripts/script\ with\ spaces.sh) or wrap the script paths in quotes (e.g. NO_RECONNECT_SCRIPT='/volume1/Scripts/script with spaces.sh'). Community-maintained scripts compatible with these features are included in the reconnect-vpn.sh Community Scripts directory.

synology-scripts's People

Contributors

ianharrier avatar ltickett avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

synology-scripts's Issues

Gateway wouldn't ping, but VPN host does

My DS is connected to another DS using OpenVPN. In this configuration the host VPN would be 10.8.0.1, the gateway 10.8.0.5, and the client DS 10.8.0.6. In that configuration the gateway would not ping, but the host (10.8.0.1) would. So... changing the second grep statement from ([0-9]+\.){3}[0-9]+ dev to ([0-9]+\.){3}[0-9]+ via would make the script work in gateway_ping mode.

local GATEWAY_IP=$(ip route | grep $TUNNEL_INTERFACE | grep -oE '([0-9]+\.){3}[0-9]+ dev' | awk '{ print $1 }' | head -n 1)

Without that modification the check would always fail, thus restart the VPN whenever the script is run...

AirVPN Stalled Connections

Hey,

Great script - works for the most part however on network disconnect where I am (hello from the Congo!) my VPN (AirVPN) maintains an Up status. So the script doesn't detect that there is an issue although network is down.

I hacked it real quick to make it work for me by replacing your Uptime check by sampling RX between a 15s sleep which so far is working quite well. Might be better to just do a ping though instead of checking uptime or RX ?

I'm not much of a bash scripter or I'd contribute more but I can get by ;)

Reconnect doesn't seem working anymore

Hello,

Script has been fine since last year but for the last couple of days something doesn't seem right. Initially VPN connections stopped working because the remote certificate had expired. Ok so that was fixed and connections seem to be working again. I had to recreate the VPN Profile but I am getting these errors now. Not sure if this is a coincidence or because of the certificate issue or because the profile was recreated.

Screen Shot 2019-09-11 at 13 27 41

The backups are being done but I am not receiving the notifications anymore. Settings haven't been changed on DSM so not sure what is going on. Ideas?

Keep trying to reconnect after a long disconnection

Hello,
I have installed this script on a DS214+ to keep a permanent VPN connection with a DS713+ for cross backup.
The VPN server is on the DS713+ and the connection is in L2TP/IPSec mode.
The script uses dsm_status only.
It works well for short duration reconnections.
However, I face an issue with long duration disconnections of the VPN server.
When 713+ is shutdown for a while (power failure for instance), the script is not able to reconnect once the server is back to work.
The 214+ VPN link stays in connection process and each attempt by the script to reconnect fails with this message : Scheduled task [Reconnect VPN] skipped because the task was already running
The only solution I have found is disconnecting the VPN to kill the connection process. It is only after the disconnection that the script is able to do its work as scheduled.
It seems that "/usr/syno/bin/synovpnc kill_client" does not kill the connection. I have tried to extend the sleep to 30 with no result.
I wonder if it would not be preferable to first attempt to reconnect with "/usr/syno/bin/synovpnc reconnect" before trying to kill ?
Thanks to have a look to this issue.
Michel

won't work for me

Hi,sorry for my bad english, i'm french, i've tested the script, and it doesen't work for me, if openvpn was connected (after a connection loss by modem for example) the script don't see this and don't reconnect

in dsm at this time vpn is connected status but no trafic is possible...

can you help me please?

Regards

oh sorry i don"t see the option for ping... yes it's good !!!

thanks

Need to add a timeout when VPN gets stuck in a reconnect

I've got a situation where when the receiving side of my VPN has a connectivity issue for an extended period of time, the calling VPN (on the Synology) ends up getting stuck on the synovpnc connect command. To the point that I end up in an endless loop of task scheduler skipping the scheduled Reconnect VPN task because it is "already running."

I think line 161 of the script should be amended as follows:

https://github.com/ianharrier/synology-scripts/blob/23b01227bd7777748bf56a42264d907504eb5c8f/reconnect-vpn.sh#L161C1-L161C48

/bin/timeout --kill-after=60 /usr/syno/bin/synovpnc connect --id=$PROFILE_ID`

The kill-after argument could be added as a CONST above as well. I'm going to create a pull request on this one. Docs on the standard GNU coreutils timeout command below.

`$ timeout --help
Usage: timeout [OPTION] DURATION COMMAND [ARG]...
  or:  timeout [OPTION]
Start COMMAND, and kill it if still running after DURATION.

Mandatory arguments to long options are mandatory for short options too.
      --preserve-status
                 exit with the same status as COMMAND, even when the
                   command times out
      --foreground
                 when not running timeout directly from a shell prompt,
                   allow COMMAND to read from the TTY and get TTY signals;
                   in this mode, children of COMMAND will not be timed out
  -k, --kill-after=DURATION
                 also send a KILL signal if COMMAND is still running
                   this long after the initial signal was sent
  -s, --signal=SIGNAL
                 specify the signal to be sent on timeout;
                   SIGNAL may be a name like 'HUP' or a number;
                   see 'kill -l' for a list of signals
  -v, --verbose  diagnose to stderr any signal sent upon timeout
      --help     display this help and exit
      --version  output version information and exit

DURATION is a floating point number with an optional suffix:
's' for seconds (the default), 'm' for minutes, 'h' for hours or 'd' for days.
A duration of 0 disables the associated timeout.

Download Station in pause

Hello,

It's not really an issue but one more feature, if possible, to add. I don't know anything about sonology script.

Is it possible to change torrent status to "pause" when it' not possuble to connect to the VPN and the opposite switch torrent "Start" when the VPN is connected.

Your script works fine !

Thanks

Default Gateway sticks

This script is great and solves the issue 90% of the time but i occasionally see an odd one where the default gateway sticks to the VPN gateway when the vpn disconnects and so when it tries to reconnect (in our case to OpenVPN cloud) it cannot because it isn't using the lan1 gateway to attempt reconnection. Wondered if anyone else has seen this too!? Ive cobbled together with my limited knowledge a script (below) which checks the default and changes it if needed but it doesn't always seem to work (it works fine when im logged in and run it, but not when its running on its own oddly, despite root being set as the run user). I put this just before '/usr/syno/bin/synovpnc kill_client'

gateway=$(ip route | awk '/default/ { print $3 }')

Check if current gateway is already LAN1

if [ "$gateway" = "$(ip addr show dev eth0 | awk '/inet/ { print $4 }' | cut -d/ -f1)" ]; then
echo "Default gateway is already set to LAN1."
exit 0
fi

Disable current default gateway

ip route del default

Set default gateway to LAN1

ip route add default via $(ip addr show dev eth0 | awk '/inet/ { print $4 }' | cut -d/ -f1)

echo "Default gateway has been set to LAN1."

Looked like it was working, but then it did not

Pulled the internet out on purpose to test if script did its job and it did, but today i updated firmware of router which means it will drop out, it does this automaticly but the script failed like 3 4 times to recognize vpn was out and did not reconnect it, i have no idea why there is no way to send a e-mail if vpn is out, so i have no way to find out what the error is.

Is there a way to use this script with multiple VPN Connections ?

Hi,
I have multiple OpenVPN connection setup in DSM for easy access to different OpenVPN Servers.
I have read that your script only works with one vpn connection added in DSM, so my question is if it is possible to alter the script to use when there are multiple VPN connections.

issue after updating to Synology DSM 7.1

After updating to Synology DSM 7.1 I had an issue with the script and did some debugging by running the script and individual lines manually using SSH and found that the below line is the issue, the script is unable to get the tunnel interface due to position change which caused the awk to return a blank value, so I changed the print $7 to $5 and the fixed the issue and command return tun0

Ex: for the $CLIENT_IP inet 10.33.33.33/24 scope global tun0

issue return blank:local TUNNEL_INTERFACE=$(ip addr | grep $CLIENT_IP | awk '{ print $7 }')

Fix return tun0:local TUNNEL_INTERFACE=$(ip addr | grep $CLIENT_IP | awk '{ print $5 }')

Error on execution

Hello.

I try to execute this script on my Synology NAS DS920+.
My version of DSM is DSM 6.2.4-25556

I have this error on the execution

reconnect-vpn.sh: line 13: $'\r': command not found
reconnect-vpn.sh: line 17: $'\r': command not found
reconnect-vpn.sh: line 23: $'\r': command not found
reconnect-vpn.sh: line 27: $'\r': command not found
reconnect-vpn.sh: line 30: $'\r': command not found
reconnect-vpn.sh: line 33: $'\r': command not found
reconnect-vpn.sh: line 37: syntax error near unexpected token `elif'

'econnect-vpn.sh: line 37: `elif [[ $CONFIGS_QTY -gt 1 ]]; then

Someone can help me ?

Have a nice day

VPN reconnect script fails

Hi,

For years now Synology has not been able to implement a proper solution for this so I've used your reconnect script before to reconnect my opvenVPN connection when it's down.

But I've noticed it's no longer working. When I manually disconnect, and then run your latest version 1.1.1 I always get an error VPN failed to reconnect, but when I establish the connection manually from DSM it works. I'm running DSM 6.2.3-25426 Update 3.

I've also regularly noticed that in DSM, the vpn stats says connected, but in fact the connection is not working (all outbound access is blocked, because I blocked all outgoing traffic from my NAS in my router, except for the openVPN port so all traffic should go through the VPN connection).

Any ideas why the script can no longer reconnect?

Add specific package stop

Hello,
Do you think you could add to your script an extra command to, in case no VPN, stop a specific package (for instance Download Station) and then try to reconnect and if successful restart the package?

Thanks
Best regards
François

Script fails on grep command

Task Scheduler has completed a scheduled task.

Task: Reconnect VPN
Start time: Fri, 24 Jun 2022 23:00:01 GMT
Stop time: Fri, 24 Jun 2022 23:01:01 GMT
Current status: 2 (Interrupted)
Standard output/error:
[I] There is 1 VPN profile. Continuing...
[I] Synology DSM reports VPN is connected.
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
[W] The gateway IP did not respond to ping.
[I] Attempting to reconnect...

kill client ...OK
get arguemnt id: o1634173141
[I] Synology DSM reports VPN is connected.
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
[W] The gateway IP did not respond to ping.
[E] VPN failed to reconnect. Exiting...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.