Skip to content

Instantly share code, notes, and snippets.

@grrowl
Created October 22, 2019 00:51
Show Gist options
  • Save grrowl/c653af224b1b070c59a9b48e076e5dce to your computer and use it in GitHub Desktop.
Save grrowl/c653af224b1b070c59a9b48e076e5dce to your computer and use it in GitHub Desktop.

Revisions

  1. grrowl revised this gist Oct 22, 2019. No changes.
  2. grrowl revised this gist Oct 22, 2019. 1 changed file with 17 additions and 35 deletions.
    52 changes: 17 additions & 35 deletions portforward.sh
    Original file line number Diff line number Diff line change
    @@ -1,38 +1,20 @@
    function portforward() {
    if [[ $# -ne 2 ]]
    #!/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
    echo "Usage: portforward HOST PORT";
    # 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
    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
    # Call script again with new local port
    "$0" "$HOST" "$REMOTE_PORT" $((RANDOM+1000))
    fi
    }

    # 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
    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="22 80 2222 3306 5432 8080 11211 55672 15672"
    COMPREPLY=($(compgen -W "${ports}" -- ${cur}))
    fi
    return 0
    }

    complete -F _portforward portforward
    fi
  3. Kura revised this gist Jun 6, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion portforward.sh
    Original 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]}"
    # 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
  4. Kura revised this gist Jun 6, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion portforward.sh
    Original 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="5432 3306 55672 15672 8080"
    ports="22 80 2222 3306 5432 8080 11211 55672 15672"
    COMPREPLY=($(compgen -W "${ports}" -- ${cur}))
    fi
    return 0
  5. Kura revised this gist Jun 6, 2013. 1 changed file with 16 additions and 16 deletions.
    32 changes: 16 additions & 16 deletions portforward.sh
    Original file line number Diff line number Diff line change
    @@ -2,24 +2,24 @@ 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
    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
    # 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
  6. Kura revised this gist Mar 1, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion portforward.sh
    Original 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" on by default
    # 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
  7. Kura revised this gist Mar 1, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions portforward.sh
    Original 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
  8. Kura created this gist Mar 1, 2013.
    38 changes: 38 additions & 0 deletions portforward.sh
    Original 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