Skip to content

Instantly share code, notes, and snippets.

@mendhak
Last active May 30, 2017 21:45
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 mendhak/ad77bddcecb344d027ba6d29134c8c96 to your computer and use it in GitHub Desktop.
Save mendhak/ad77bddcecb344d027ba6d29134c8c96 to your computer and use it in GitHub Desktop.
Reliable methods of creating SSH tunnels for TCP forwarding
#Source: https://stackoverflow.com/questions/2241063/bash-script-to-setup-a-temporary-ssh-tunnel
#SSH feature - control sockets
ssh -4 -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa -M -S my-ctrl-socket -fnNT -L 10023:remoteserver.com:8000 dev@middleserver.internal
#Get PID - you'll have to parse this
ssh -S my-ctrl-socket -O check dev@middleserver.internal
#Exit
ssh -S my-ctrl-socket -O exit dev@middleserver.internal
#-------------
# Another way without control sockets
ssh -4 -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa -Cfo ExitOnForwardFailure=yes -NL 10023:remoteserver.com:8000 dev@middleserver.internal
PID=$(pgrep -f 'NL 10023')
[ "$PID" ] || exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment