不久之前,我得到了一个答案,告诉我如何在正在运行的SSH ControlMaster进程中添加端口转发。 要知道这有很大的帮助,但我仍然错过了一个方法来消除这样的端口转发后,我不再需要了。
据我所知,你可以通过正常连接的内部命令键顺序来做到这一点,这似乎是被禁用的ControlMaster客户端。 即使这是可能的,我需要一个解决scheme,我可以用脚本自动化,这肯定不是那么容易。
有没有办法做到这一点? 它很容易自动化吗?
正如我以前的答案指出的那样, ssh手册页详细解释了-O ctl_cmd ,其中ctl_cmd是check , forward , cancel或exit 。
-O ctl_cmd Control an active connection multiplexing master process. When the -O option is specified, the ctl_cmd argument is interpreted and passed to the master process. Valid commands are: “check” (check that the master process is running), “forward” (request forwardings without command execution), “exit” (request the master to exit), and “stop” (request the master to stop accepting further multiplexing requests).
您可以使用ctl_cmd来完成您想要的脚本自动化。 你需要首先创build一个带有-S和ssh的ControlMaster套接字,如下所示:
ssh -v -M -S/tmp/controlmaster-remotehost remotehost
然后从您的本地机器,您可以forward和cancel端口,只要你喜欢。 您可以使用一个命令转发多个端口:
ssh -O forward -S/tmp/controlmaster-remotehost -L5555:localhost:22 -L3333:localhost:22 remotehost
和/或一次处理一个:
ssh -O cancel -S/tmp/controlmaster-remotehost -L5555:localhost:22 remotehost ssh -O cancel -S/tmp/controlmaster-remotehost -L3333:localhost:22 remotehost
我创build了一个并行会话的主题,显示了ssh -O ctl_cmd的作用; 端口forward / cancel从本地主机在左侧,和SSH的输出连接到右侧的远程主机:
这些命令仅适用于OpenSSH 6.0 :
* ssh(1): support for cancelling local and remote port forwards via the multiplex socket. Use ssh -O cancel -L xx:xx:xx -R yy:yy:yy user@host" to request the cancellation of the specified forwardings * support cancellation of local/dynamic forwardings from ~C commandline
如果你有一个更早的版本,你需要升级。 如果您在Mac OS X上,则可以安装macports ,然后使用以下sudo port install openssh进行升级 : sudo port install openssh ,将其安装到/opt/local/bin 。