Last active
May 31, 2020 20:18
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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