Skip to content

Instantly share code, notes, and snippets.

@grambas
Forked from codeinthehole/portforward.sh
Last active January 23, 2024 10:04
Show Gist options
  • Save grambas/8f983a73f999619cfd7a32789b3c7920 to your computer and use it in GitHub Desktop.
Save grambas/8f983a73f999619cfd7a32789b3c7920 to your computer and use it in GitHub Desktop.

Revisions

  1. grambas revised this gist Jan 23, 2024. 1 changed file with 37 additions and 11 deletions.
    48 changes: 37 additions & 11 deletions portforward.sh
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,47 @@
    # add these functions in ~/.bashrc
    #
    # Easier port forwarding - usage:
    #
    # $ portforward project-app1 5432
    # $ portforward 9204 stageX localhost 9200
    #
    # where 'stageX' is an alias from ~/.ssh/config
    # 9200 is the remote port on localhost
    # 9204 is local port
    #
    # where 'project-app1' is an alias from ~/.ssh/config and 5432 is the remote
    # port that you want to forward to.
    function portforward() {
    HOST=$1
    REMOTE_PORT=$2
    # Pick a random port and check it is free
    LOCAL_PORT=$((RANDOM+1000))
    if [[ $# -le 1 ]]
    then
    echo "Usage: portforward LOCAL_PORT HOST REMOTE_HOST REMOTE_PORT";
    return
    fi

    LOCAL_PORT=$1
    HOST=$2
    REMOTE_HOST=$3
    REMOTE_PORT=$4

    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 -L $LOCAL_PORT:localhost:$REMOTE_PORT $HOST -N 2> /dev/null
    echo "ssh -L $LOCAL_PORT:$REMOTE_HOST:$REMOTE_PORT $HOST -N"
    ssh -L $LOCAL_PORT:$REMOTE_HOST:$REMOTE_PORT $HOST -N 2> /dev/null
    [[ $? -ne 0 ]] && echo "Unable to connect to $HOST"
    else
    # Recursion ftw
    # Recursion ftw
    portforward $HOST $REMOTE_PORT
    fi
    }
    }

    function _portforward() {
    # Only autocomplete on the first arg
    if [[ ${COMP_WORDS[COMP_CWORD-1]} == "portforward" ]]
    then
    # Extract host aliases from ~/.ssh/config
    HOSTS=$(grep --color=never "^Host " ~/.ssh/config | awk '{if ($2 != "*") print $2}')
    CURRENT_WORD="${COMP_WORDS[COMP_CWORD]}"
    COMPREPLY=($(compgen -W "$HOSTS" -- $CURRENT_WORD))
    return 0
    fi
    }

    complete -F _portforward portforward
  2. @codeinthehole codeinthehole revised this gist Mar 1, 2013. No changes.
  3. @codeinthehole codeinthehole renamed this gist Mar 1, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @codeinthehole codeinthehole created this gist Mar 1, 2013.
    21 changes: 21 additions & 0 deletions portforward
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    # Easier port forwarding - usage:
    #
    # $ portforward project-app1 5432
    #
    # where 'project-app1' is an alias from ~/.ssh/config and 5432 is the remote
    # port that you want to forward to.
    function portforward() {
    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 -L $LOCAL_PORT:localhost:$REMOTE_PORT $HOST -N 2> /dev/null
    else
    # Recursion ftw
    portforward $HOST $REMOTE_PORT
    fi
    }