如何重用 ssh 连接 [英] How to reuse an ssh connection
问题描述
我正在创建一个小脚本来更新一些远程服务器(2+)
我正在与每个服务器建立多个连接;有没有办法可以重复使用 SSH 连接,这样我就不必一次打开太多?
I'm creating a small script to update some remote servers (2+)
I am making multiple connects to each server; is there a way I can reuse the SSH connections so I don't have to open too many at once?
推荐答案
如果用-M
打开第一个连接:
If you open the first connection with -M
:
ssh -M $REMOTEHOST
到$REMOTEHOST
的后续连接将捎带"在由主ssh
建立的连接上.最值得注意的是,不需要进一步的身份验证.有关详细信息,请参阅ControlMaster"下的 man ssh_config
.使用-S
指定共享socket的路径;我不确定默认值是什么,因为我使用配置文件来配置连接共享.
subsequent connections to $REMOTEHOST
will "piggyback" on the connection established by the master ssh
. Most noticeably, further authentication is not required. See man ssh_config
under "ControlMaster" for more details. Use -S
to specify the path to the shared socket; I'm not sure what the default is, because I configure connection sharing using the configuration file instead.
在我的 .ssh/config
文件中,我有以下几行:
In my .ssh/config
file, I have the following lines:
host *
ControlMaster auto
ControlPath ~/.ssh/ssh_mux_%h_%p_%r
这样,我就不用记得用-M
或-S
;ssh
确定主机/端口/用户名组合是否已存在可共享连接,并在可能的情况下使用该连接.
This way, I don't have to remember to use -M
or -S
; ssh
figures out if a sharable connection already exists for the host/port/username combination and uses that if possible.
该选项自 2004 年起在 OpenSSH 中可用.
This option is available in OpenSSH since 2004.
这篇关于如何重用 ssh 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!