Skip to content

Instantly share code, notes, and snippets.

@numpde
Last active January 17, 2018 12:35
Show Gist options
  • Save numpde/bbae9dc7aba78eb59b87588c07d83687 to your computer and use it in GitHub Desktop.
Save numpde/bbae9dc7aba78eb59b87588c07d83687 to your computer and use it in GitHub Desktop.
Create an ssh tunnel using a local port
#!/bin/bash
# Suppose you wish to connect to a server X on port 23
# but you have to do this via another server Y like this:
#
# localhost> ssh -p 22 username@Y
# Y> ssh -p 23 username@X
#
# Instead, you can create an ssh tunnel on a local port, say 9999:
# localhost> ssh -nNT -L 9999:X:23 -p 22 username@Y
# (assumes you are using bash)
#
# Then you can connect to the server X as follows:
# localhost> ssh -p 9999 username@localhost
pL=9999
X=X
pX=23
Y=Y
pY=22
user=username
ssh -nNT -L $pL:$X:$pX -p $pY $user@$Y & echo Tunnel PID: $!
echo Connect using:
echo ssh -p $pL $user@localhost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment