Skip to content

Instantly share code, notes, and snippets.

@nopivnick
Created September 26, 2013 20:03
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 nopivnick/6719772 to your computer and use it in GitHub Desktop.
Save nopivnick/6719772 to your computer and use it in GitHub Desktop.
A bash script for keeping a reverse SSH tunnel initiated by a machine behind a firewall persistent. If you wanted multiple tunnels (i.e., to multiple hosts) you might paramaterize this then have separate scripts named for each host (e.g., ssh-reverse-persist_host-1.sh, ssh-reverse-persist_host-2.sh) that called ssh-reverse-persist.sh.
#!/bin/bash
createTunnel() {
/usr/bin/ssh -t -t -R 19999:localhost:22 user@example.com
if [[ $? -eq 0 ]]; then
echo Tunnel to example.com created successfully
else
echo An error occurred creating a tunnel to example.com RC was $?
fi
}
## to see if the tunnel is running. If it returns non-zero, then create a new connection
ps x | grep "19999\:localhost\:22"
if [[ $? -ne 0 ]]; then
echo Creating new tunnel connection
createTunnel
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment