Skip to content

Instantly share code, notes, and snippets.

@hongster
Created December 21, 2023 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hongster/5b9b44b1edeb75c93f9c9c9119a7ca1b to your computer and use it in GitHub Desktop.
Save hongster/5b9b44b1edeb75c93f9c9c9119a7ca1b to your computer and use it in GitHub Desktop.
Reestablish SSH tunnel connection when disconnected.
#!/usr/bin/env bash
remoteUser="foobar"
remoteHost="example.com"
count=$(pgrep -U $(id -u) -a -f "$remoteHost" -c)
if [[ $count -eq 0 ]];then
echo "Restarting SSH runnel"
ssh -f -N -R 2022:localhost:22 ${remoteUser}@${remoteHost} -o ServerAliveInterval=30 -o ServerAliveCountMax=1 -o ExitOnForwardFailure=yes
else
echo "SSH tunnel running"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment