Skip to content

Instantly share code, notes, and snippets.

@dwf
Created October 9, 2016 18:28
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 dwf/f3970c1e7f40a859fda8e396362792b8 to your computer and use it in GitHub Desktop.
Save dwf/f3970c1e7f40a859fda8e396362792b8 to your computer and use it in GitHub Desktop.
Easily forward ports through an SSH login node.
sshft() {
if [ $# -lt 2 ]; then
echo "usage: sshft host [port|local:remote|local:bridge:remote] ...]"
return 1
fi
if [ -z "$DEFAULT_SSH_PROXY_HOST" ]; then
echo "No DEFAULT_SSH_PROXY_HOST set."
return 1
fi
FIRST_SSH_ARGS="$DEFAULT_SSH_PROXY_HOST"
SECOND_SSH_ARGS="$1"
shift
while (( "$#" )); do
if [[ "$1" =~ ^[0-9]+$ ]]; then
FIRST_SSH_ARGS="$FIRST_SSH_ARGS -L $1:localhost:$1"
SECOND_SSH_ARGS="$SECOND_SSH_ARGS -L $1:localhost:$1"
elif [[ "$1" =~ ^[0-9]+:[0-9]+$ ]]; then
FIRST_PORT=$(echo $1 |cut -d ':' -f 1)
SECOND_PORT=$(echo $1 |cut -d ':' -f 2)
FIRST_SSH_ARGS="$FIRST_SSH_ARGS -L $FIRST_PORT:localhost:$FIRST_PORT"
SECOND_SSH_ARGS="$SECOND_SSH_ARGS -L $FIRST_PORT:localhost:$SECOND_PORT"
elif [[ "$1" =~ ^[0-9]+:[0-9]+:[0-9]+$ ]]; then
FIRST_PORT=$(echo $1 |cut -d ':' -f 1)
SECOND_PORT=$(echo $1 |cut -d ':' -f 2)
THIRD_PORT=$(echo $1 |cut -d ':' -f 3)
FIRST_SSH_ARGS="$FIRST_SSH_ARGS -L $FIRST_PORT:localhost:$SECOND_PORT"
SECOND_SSH_ARGS="$SECOND_SSH_ARGS -L $SECOND_PORT:localhost:$THIRD_PORT"
else
echo "usage: sshft host [port|local:remote|local:bridge:remote] ...]"
return 1
fi
shift
done
echo ssh $FIRST_SSH_ARGS -t ssh $SECOND_SSH_ARGS
ssh $FIRST_SSH_ARGS -t ssh $SECOND_SSH_ARGS
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment