Skip to content

Instantly share code, notes, and snippets.

@tsabat
Last active March 26, 2020 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tsabat/5587052 to your computer and use it in GitHub Desktop.
Save tsabat/5587052 to your computer and use it in GitHub Desktop.
port forwarding bash script

We hide the services behind an AWS Security Group. But, we can use some trusty SSH action to forward localhost ports to those on the Solr server.

After you've added this to your ~/.zshrc or ~/.bash_profile and sourced (source ~/.bash_profile) it, you can forward these ports from localhost to your server like so:

portforward <hostname>

zsh and bash handle string splitting differently, so be sure to choose correctly for your shell.

For zsh:

SOLR_PORTS=(8080 9292 2812)

for bash:

SOLR_PORTS="8080 9292 2812"

for both shells

function portforward() {
  SERVER=$1
  for port in $SOLR_PORTS
  do
    echo "forwarding $port"
    ssh -l ubuntu $SERVER -p 22 -N -f -C -L $port":localhost:"$port
  done
}

function portforwardend() {
  for port in $SOLR_PORTS
  do
    spec="\-L $port"":localhost:"$port
    echo "unforwarding $spec"
    ps aux | grep $spec | grep -v grep | awk '{print $2}' | xargs kill
  done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment