Skip to content

Instantly share code, notes, and snippets.

@21isenough
Created May 10, 2020 09:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 21isenough/9bce7d944a1d785aa42d6918752c7025 to your computer and use it in GitHub Desktop.
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)
#/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