Created
September 6, 2013 17:11
-
-
Save xtman/6466767 to your computer and use it in GitHub Desktop.
A shell script to establish a ssh tunnel using ssh command.
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
#!/bin/sh | |
LOCAL_HOST="localhost" | |
LOCAL_PORT="" | |
GATEWAY_USER="" | |
GATEWAY_HOST="" | |
GATEWAY_PORT=22 | |
TARGET_HOST="" | |
TARGET_PORT="" | |
SSH_CTRL_DIR="/tmp" | |
SSH_CTRL_PATH="" | |
function usage() { | |
echo "Usage:" | |
echo " $(basename $0) [-lh localhost] -lp <local-port> -gu <gateway-user> -gh <gateway-host> [-gp gateway-port] -th <target-host> -tp <target-port>" | |
echo "Options:" | |
echo " -lh | --local-host \t The address of the local host. Defaults to localhost if not specified." | |
echo " -lp | --local-port \t The port of the local host to map to. Must be specified." | |
echo " -gu | --gateway-user \t The username to access the gateway host. Must be specified." | |
echo " -gh | --gateway-host \t The address of the gateway host. Must be specified." | |
echo " -gp | --gateway-port \t The address of the gateway port. Defaults to 22 if not specified." | |
echo " -th | --target-host \t The address of the target host. Must be specified." | |
echo " -tp | --target-port \t The port of the target host. Must be specified." | |
echo "Examples:" | |
echo " 1) tunnel from 192.168.20.8:3389 to localhost:3389 via gateway gateway.yourcompany.org" | |
echo " $(basename $0) -lp 3389 -gu john -gh gateway.yourcompany.org -th 192.168.20.8 -tp 3389" | |
echo " 2) tunnel from 192.168.20.8:22 to localhost:22222 via gateway gateway.yourcompany.org" | |
echo " $(basename $0) -lp 22222 -gu john -gh gateway.yourcompany.org -th 192.168.20.8 -tp 22" | |
} | |
# parse arguments | |
while [ $# -gt 0 ] | |
do | |
case $1 in | |
-lh|--local-host) | |
[[ -z $2 ]] && echo "Error: missing --local-host value." 2>&1 && usage && exit 1 | |
LOCAL_HOST=$2; shift | |
;; | |
-lp|--local-port) | |
[[ -z $2 ]] && echo "Error: missing --local-port value." 2>&1 && usage && exit 1 | |
LOCAL_PORT=$2; shift | |
;; | |
-gu|--gateway-user) | |
[[ -z $2 ]] && echo "Error: missing --gateway-user value." 2>&1 && usage && exit 1 | |
GATEWAY_USER=$2; shift | |
;; | |
-gh|--gateway-host) | |
[[ -z $2 ]] && echo "Error: missing --gateway-host value." 2>&1 && usage && exit 1 | |
GATEWAY_HOST=$2; shift | |
;; | |
-gp|--gateway-port) | |
[[ -z $2 ]] && echo "Error: missing --gateway-port value." 2>&1 && usage && exit 1 | |
GATEWAY_PORT=$2; shift | |
;; | |
-th|--target-host) | |
[[ -z $2 ]] && echo "Error: missing --target-host value." 2>&1 && usage && exit 1 | |
TARGET_HOST=$2; shift | |
;; | |
-tp|--target-port) | |
[[ -z $2 ]] && echo "Error: missing --target-port value." 2>&1 && usage && exit 1 | |
TARGET_PORT=$2; shift | |
;; | |
*) | |
break | |
;; | |
esac | |
shift | |
done | |
# check arguments | |
[[ -z $LOCAL_PORT ]] && echo "Error: --local-port is not specified." 2>&1 && usage && exit 1 | |
[[ -z $GATEWAY_USER ]] && echo "Error: --gateway-user is not specified." 2>&1 && usage && exit 1 | |
[[ -z $GATEWAY_HOST ]] && echo "Error: --gateway-host is not specified." 2>&1 && usage && exit 1 | |
[[ -z $TARGET_HOST ]] && echo "Error: --target-host is not specified." 2>&1 && usage && exit 1 | |
[[ -z $TARGET_PORT ]] && echo "Error: --target-port is not specified." 2>&1 && usage && exit 1 | |
SSH_CTRL_PATH="${SSH_CTRL_DIR}/ssh-tunnel.${LOCAL_HOST}.${LOCAL_PORT}" | |
if [[ -z $GATEWAY_PORT ]]; then | |
CMD="ssh -L ${LOCAL_HOST}:${LOCAL_PORT}:${TARGET_HOST}:${TARGET_PORT} ${GATEWAY_USER}@${GATEWAY_HOST} -N -f -M -S ${SSH_CTRL_PATH}" | |
else | |
CMD="ssh -L ${LOCAL_HOST}:${LOCAL_PORT}:${TARGET_HOST}:${TARGET_PORT} ${GATEWAY_USER}@${GATEWAY_HOST} -p ${GATEWAY_PORT} -N -f -M -S ${SSH_CTRL_PATH}" | |
fi | |
$CMD |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment