Quite a while ago I got an answer telling me how to add a port-forwarding on a running SSH ControlMaster process. To know that helps a lot, but I'm still missing a way to remove such a port forwarding after I don't need that anymore.
As far as I know, you can do that through the internal command key sequence on normal connections, this seems to be disabled for ControlMaster clients. Even if that would be possible I would need a solution which I can automatize with scripts, which is surely not so easy this way.
Is there a way to do it? And is it easily automatizable?
As my previous answer notes, the ssh man page explains the
-O ctl_cmd
in detail, wherectl_cmd
is one ofcheck
,forward
,cancel
, orexit
.You can use the
ctl_cmd
's to accomplish the automation you want for your scripts. You'll need to first create aControlMaster
socket with the-S
like and ssh to the remotehost like this:and then from your local machine you can
forward
andcancel
ports as you like. You can forward multiple ports with one command:and/or handle them one at a time:
I created a gist with a side-by-side session showing the
ssh -O ctl_cmd
in action; with portforward
/cancel
from localhost on the left side, and the output of ssh connected to remotehost on the right side:https://gist.github.com/raw/4265549/999d90b8f85190a41eed00e4d60d5d22da6c67b9/ssh-controlmaster-side-by-side.log
These commands are only available as of OpenSSH 6.0:
If you have an earlier version you'll need to upgrade. If you're on Mac OS X you can install macports and then upgrade using:
sudo port install openssh
which will install it into/opt/local/bin
.