Last active
August 28, 2017 16:40
-
-
Save nhoffman/8636c6e267cb4403ac3a35ff0bc53f3d to your computer and use it in GitHub Desktop.
open an ssh tunnel and point a browser to it
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/bash | |
# Requires argparse.bash (https://github.com/nhoffman/argparse-bash) | |
# in the same directory as this script | |
# Installation: | |
# cd ~/bin # or another location on your $PATH | |
# curl https://gist.githubusercontent.com/nhoffman/8636c6e267cb4403ac3a35ff0bc53f3d/raw/ > ssh-tunnel | |
# chmod +x ssh-tunnel | |
# curl -O https://raw.githubusercontent.com/nhoffman/argparse-bash/master/argparse.bash | |
source $(dirname $0)/argparse.bash || exit 1 | |
argparse "$@" <<EOF || exit 1 | |
parser.add_argument('host') | |
parser.add_argument('-r', '--remote-port', default='8000') | |
parser.add_argument('-l', '--local-port') | |
parser.add_argument('-n', '--no-open-browser', dest='open_browser', action='store_false', | |
default=True, help="open browser to localhost:<local-port> (mac only)") | |
parser.add_argument('-v', '--verbose', dest='verbose', | |
action='store_true', default=False) | |
EOF | |
if [[ -z $LOCAL_PORT ]]; then | |
LOCAL_PORT=$REMOTE_PORT | |
fi | |
# echo $HOST | |
# echo $REMOTE_PORT | |
# echo $LOCAL_PORT | |
# echo $OPEN_BROWSER | |
delay=5 | |
echo "forwarding $HOST:$REMOTE_PORT to localhost:$LOCAL_PORT " | |
if [[ $OPEN_BROWSER ]]; then | |
echo "opening browser in $delay seconds" | |
(sleep $delay; open http://localhost:$LOCAL_PORT) & | |
fi | |
if [[ $VERBOSE ]]; then | |
verbose="-v" | |
echo ssh -o ForwardX11=no $verbose -C -nNT -L $LOCAL_PORT:localhost:$REMOTE_PORT "$HOST" | |
fi | |
# note that we deliberately leave the ssh process in the foreground so | |
# that we can kill with ^C | |
ssh -o ForwardX11=no $verbose -C -nNT -L $LOCAL_PORT:localhost:$REMOTE_PORT "$HOST" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment