Created
May 10, 2020 09:44
-
-
Save 21isenough/9bce7d944a1d785aa42d6918752c7025 to your computer and use it in GitHub Desktop.
Check if SSH still up, reconnect if not (run as a cron job)
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
#/bin/bash | |
DATE=$(date) | |
runAutossh() { | |
/usr/bin/autossh -M 20000 -Nf -R PORT:127.0.0.1:PORT USER@REMOTE_IP | |
if [[ $? -eq 0 ]]; then | |
echo Tunnel to REMOTE_IP created successfully | |
else | |
echo An error occurred creating a tunnel | |
fi | |
} | |
## Check if PORT can be reached. If it returns non-zero, then create a new connection | |
/bin/nc -z -v -w5 REMOTE_IP PORT | |
if [[ $? -ne 0 ]]; then | |
echo Creating new tunnel connection | |
echo "Connection down: ${DATE} > Tunnel restart" >> /home/USER/tunnel_reconnect.log | |
runAutossh | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment