Skip to content

Instantly share code, notes, and snippets.

@sean-m
Last active May 31, 2020 20:18
Show Gist options
  • Save sean-m/d2351d77c1e70cdc3d6f508de2b55e5d to your computer and use it in GitHub Desktop.
Save sean-m/d2351d77c1e70cdc3d6f508de2b55e5d to your computer and use it in GitHub Desktop.
Simple script for tunneling ports over ssh so I don't have to look up the ssh switches each time.
#!/usr/bin/env bash
showhelp=0
verbose=0
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-u|--user)
user="$2"
shift # past argument
shift # past value
;;
-t|--target)
target="$2"
shift # past argument
shift # past value
;;
-p|--port)
port="$2"
shift # past argument
shift # past value
;;
-h|--help)
showhelp=1
shift # past argument
shift # past value
;;
-v|--verbose)
verbose=1
shift # past argument
shift # past value
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
if [ $showhelp -eq 1 ]; then
echo "Cheater way of tunneling a tcp port on a remote machine to a localhost port with ssh."
echo "The same port number is used at each end so just read the ssh man page and do it"
echo "yourself if that doesn't work for you."
echo ""
echo "Usage:"
echo " ${0} -u username -t target -p portnumber"
exit
fi
argstring=""
argstring+="-L $port:localhost:$port -N -C -l $user $target"
if [ $verbose -eq 1 ]; then
echo "target: $target"
echo "port: $port"
echo "user: $user"
argstring+=" -v"
fi
ssh $argstring $POSITIONAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment