Answers:
从手册:
-O ctl_cmd
控制活动连接多路复用主进程。当-O
指定选项时,ctl_cmd
参数被解释并传递给主进程。有效命令包括:(check
检查主进程是否正在运行),forward
(在不执行命令的情况下请求转发),cancel
(取消转发),exit
(请求主进程退出)和stop
(请求主进程停止接受进一步的多路复用请求)。
较早的版本仅包含check
和exit
,但这足以满足您的目的。
ssh -O check host.example.com
如果要一口气删除所有连接(而不仅仅是到特定主机的连接),则fuser /tmp/ssh_mux_*
或lsof /tmp/ssh_mux_*
将列出控制每个套接字的ssh客户端。用于fuser -HUP -k tmp/ssh_mux_*
彻底清除它们(使用SIGHUP作为信号是最好的,因为它可以使客户端正确地卸下其套接字)。
check
比我一直在寻找的东西有用,exit
反正是这一切!:D
fuser
中无法发送信号,但是效果也一样:lsof -Fp /tmp/ssh_mux_* | cut -c 2- | xargs kill -HUP
我写了一个开源实用程序cmc
来管理ControlMaster会话:ClockworkNet / cmc:
Usage: cmc [ -c HOST | -o HOST | -x HOST ]
cmc [ -L | -l | -O | -X ]
cmc -h
ControlMaster Controller - Eases management of SSH ControlMaster connections
Options:
-h show this help message and exit
-c HOST check HOST ControlMaster connection status (maybe specified more
than once)
-L list ControlMasters defined in SSH_CONFIG
-l list ControlMaster connection sockets in ~/.ssh/ and check their
connection status
-O open all ControlMasters defined in SSH_CONFIG
-o HOST open a ControlMaster session (maybe specified more than once)
-x HOST close ControlMaster session (maybe specified more than once)
-X exit all ControlMaster connections with sockets in ~/.ssh/
Notes:
* Any unopened sockets in ~/.ssh/ are removed with -l and -X
这仅对控制主机使用套接字文件对我有用:
$ ssh -o ControlPath=~/.ssh/<controlfile> -O check <bogus arg>
这是一个已经建立与远程服务器的连接的示例:
$ ssh -o ControlPath=~/.ssh/master-57db26a0499dfd881986e23a2e4dd5c5c63e26c2 -O check blah
Master running (pid=89228)
$
并断开连接:
$ ssh -o ControlPath=~/.ssh/master-66496a62823573e4760469df70e57ce4c15afd74 -O check blah
Control socket connect(/Users/user1/.ssh/master-66496a62823573e4760469df70e57ce4c15afd74): No such file or directory
$
如果仍然连接,这将迫使它立即退出:
$ ssh -o ControlPath=~/.ssh/master-66496a62823573e4760469df70e57ce4c15afd74 -O exit blah
Exit request sent.
$
对我来说尚不清楚,但它似乎可能是一个错误,ssh
因为它在末尾需要一个附加参数,即使blah
在我使用的开关的上下文中这毫无意义。
没有它,这给了我:
$ ssh -o ControlPath=~/.ssh/master-57db26a0499dfd881986e23a2e4dd5c5c63e26c2 -O check
usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-E log_file] [-e escape_char]
[-F configfile] [-I pkcs11] [-i identity_file]
[-L [bind_address:]port:host:hostport] [-l login_name] [-m mac_spec]
[-O ctl_cmd] [-o option] [-p port]
[-Q cipher | cipher-auth | mac | kex | key]
[-R [bind_address:]port:host:hostport] [-S ctl_path] [-W host:port]
[-w local_tun[:remote_tun]] [user@]hostname [command]
$ ssh -V
OpenSSH_6.9p1, LibreSSL 2.1.8
CentOS 7.x
$ ssh -V
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017
我确认,在这两个版本中,都需要附加的虚假参数。
ssh -S none
(也许这已经对您有所帮助)。