2018
03.29

ssh master just for the ports

Hey all

decided to write about something that i use way more often than i care about and have shown others on how to do.

sample from my ssh config:
Host foo
Hostname ADDRESS1
LocalForward 5432 ADDRESS2:5432
LocalForward 7777 ADDRESS3:8080
IdentityFile ~/.ssh/key.pem
User poa

now lets say i do not want to open an ssh shell but i do need those ports forwarded locally, then lets use some ssh master like such:
ssh -f -N -M -S /tmp/file-sock foo

this will forward the ports without needing to keep a terminal open.

to exit this:
ssh -S /tmp/file-sock -O exit foo

and done.

now lets say that you want to automate this into functions so that you can use this with multiple host names, then all you do is add these lines to either your .zshrc or .bashrc files:

function tunnel() { if [ -z $1 ] ; then echo "need hostname" ; else ssh -f -N -M -S /tmp/file-${1} ${1} ; fi }
function killtunnel() { if [ -z $1 ] ; then echo "need hostname" ; else ssh -S /tmp/file-${1} -O exit ${1} ; fi }

so now when you run tunnel foo it will start it up and create an appropriately named sock file. to kill it you just run killtunnel foo

voila.

The following two tabs change content below.

charlie root

VP of keeping it real / HNIC at pissedoffadmins.com
I bring the cold coffee.

Latest posts by charlie root (see all)

No Comment.

Add Your Comment