Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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