-
-
Save grambas/8f983a73f999619cfd7a32789b3c7920 to your computer and use it in GitHub Desktop.
bashrc port forward helper
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
| # 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 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment