Created
October 22, 2019 00:51
-
-
Save grrowl/c653af224b1b070c59a9b48e076e5dce to your computer and use it in GitHub Desktop.
Revisions
-
grrowl revised this gist
Oct 22, 2019 . No changes.There are no files selected for viewing
-
grrowl revised this gist
Oct 22, 2019 . 1 changed file with 17 additions and 35 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,38 +1,20 @@ #!/bin/bash if [[ $# -lt 2 ]] then echo "Usage: port-forward HOST REMOTE_PORT [LOCAL_PORT]"; else HOST=$1 REMOTE_PORT=$2 LOCAL_PORT=${3:-${REMOTE_PORT}} if ! [[ $(lsof -i :$LOCAL_PORT | grep COMMAND) ]] then # Port is free, set up tunnel echo "Forwarding to port $REMOTE_PORT on $HOST from http://localhost:$LOCAL_PORT" ssh -f -L "$LOCAL_PORT:localhost:$REMOTE_PORT" "$HOST" -N 2> /dev/null else # Call script again with new local port "$0" "$HOST" "$REMOTE_PORT" $((RANDOM+1000)) fi fi -
Kura revised this gist
Jun 6, 2013 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -22,7 +22,6 @@ function portforward() { # Used for autocompletion function _portforward() { cur="${COMP_WORDS[COMP_CWORD]}" if [[ ${COMP_WORDS[COMP_CWORD-1]} == "portforward" ]] then # the sed call is there to combat people like me who have "grep --color=always" on by default -
Kura revised this gist
Jun 6, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -30,7 +30,7 @@ function _portforward() { COMPREPLY=($(compgen -W "${hosts}" -- ${cur})) else # otherwise assume on second arg, so autocomplete service name ports="22 80 2222 3306 5432 8080 11211 55672 15672" COMPREPLY=($(compgen -W "${ports}" -- ${cur})) fi return 0 -
Kura revised this gist
Jun 6, 2013 . 1 changed file with 16 additions and 16 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -2,24 +2,24 @@ function portforward() { if [[ $# -ne 2 ]] then echo "Usage: portforward HOST PORT"; else HOST=$1 REMOTE_PORT=$2 # Pick a random port and check it is free LOCAL_PORT=$((RANDOM+1000)) if ! [[ `lsof -i :$LOCAL_PORT | grep COMMAND` ]] then # Port is free - woop! echo "Forwarding to port $REMOTE_PORT on $HOST from http://localhost:$LOCAL_PORT" ssh -f -L $LOCAL_PORT:localhost:$REMOTE_PORT $HOST -N 2> /dev/null else # Recursion ftw portforward $HOST $REMOTE_PORT fi fi } # Used for autocompletion function _portforward() { cur="${COMP_WORDS[COMP_CWORD]}" # COMP_CWORD-1 = portforward, i.e. the name of this function, so autocomplete SSH host @@ -35,5 +35,5 @@ function _portforward() { fi return 0 } complete -F _portforward portforward -
Kura revised this gist
Mar 1, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -25,7 +25,7 @@ function _portforward() { # COMP_CWORD-1 = portforward, i.e. the name of this function, so autocomplete SSH host if [[ ${COMP_WORDS[COMP_CWORD-1]} == "portforward" ]] then # the sed call is there to combat people like me who have "grep --color=always" on by default hosts=$(grep "Host " ~/.ssh/config | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | grep -v "#" | grep -v "*" | awk '{print $2}') COMPREPLY=($(compgen -W "${hosts}" -- ${cur})) else -
Kura revised this gist
Mar 1, 2013 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -25,6 +25,7 @@ function _portforward() { # COMP_CWORD-1 = portforward, i.e. the name of this function, so autocomplete SSH host if [[ ${COMP_WORDS[COMP_CWORD-1]} == "portforward" ]] then # the sed call is there to combat people like me who have "grep --color" on by default hosts=$(grep "Host " ~/.ssh/config | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | grep -v "#" | grep -v "*" | awk '{print $2}') COMPREPLY=($(compgen -W "${hosts}" -- ${cur})) else -
Kura created this gist
Mar 1, 2013 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ function portforward() { if [[ $# -ne 2 ]] then echo "Usage: portforward HOST PORT"; exit; fi HOST=$1 REMOTE_PORT=$2 # Pick a random port and check it is free LOCAL_PORT=$((RANDOM+1000)) if ! [[ `lsof -i :$LOCAL_PORT | grep COMMAND` ]] then # Port is free - woop! echo "Forwarding to port $REMOTE_PORT on $HOST from http://localhost:$LOCAL_PORT" ssh -f -L $LOCAL_PORT:localhost:$REMOTE_PORT $HOST -N 2> /dev/null else # Recursion ftw portforward $HOST $REMOTE_PORT fi } # Used for autocompletion function _portforward() { cur="${COMP_WORDS[COMP_CWORD]}" # COMP_CWORD-1 = portforward, i.e. the name of this function, so autocomplete SSH host if [[ ${COMP_WORDS[COMP_CWORD-1]} == "portforward" ]] then hosts=$(grep "Host " ~/.ssh/config | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | grep -v "#" | grep -v "*" | awk '{print $2}') COMPREPLY=($(compgen -W "${hosts}" -- ${cur})) else # otherwise assume on second arg, so autocomplete service name ports="5432 3306 55672 15672 8080" COMPREPLY=($(compgen -W "${ports}" -- ${cur})) fi return 0 } complete -F _portforward portforward