我刚刚发现了OpenSSH的ControlMaster / ControlPathfunction,它允许您使用单个SSH连接来运行多个terminal。
由于我经常使用SSH来使用端口转发来获得encryption和authentication的VNC会话,所以我立即发现您不能将端口转发添加到已经build立了连接的远程服务器。 这很糟糕。
有时后来我发现你可以通过在正在运行的SSHterminal会话中input〜C来绕过这个限制。 这将打开一个命令行,允许您添加或删除端口转发。
我现在的问题是:如何在使用ControlMaster / ControlPathfunction的现有SSH会话上添加端口转发,而无需访问该SSH会话内的terminal会话。 我需要这个启用我的脚本,启动一个安全的隧道VNC连接,我添加并稍后删除其端口转发。
(我知道我可以使用terminal多路复用器,比如GNU Screen或者tmux,其实我已经这么做了,但是我喜欢用一个SSH会话的想法。
实际上这很简单。 只需将ctl_cmd -O forward到您现有的命令,即:
ssh -M -L5555:localhost:22 remotehost
变为:
ssh -O forward -M -L5555:localhost:22 remotehost
ssh手册页讨论了-O ctl_cmd选项:
-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).
这当然假设你已经在~/ssh/config文件中启用了ControlMaster yes ,或者在命令行中启用了-M 。