You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ruby的Net::SSH能否像ControlMaster那样复用SSH连接?

让Ruby的Net::SSH复用ControlMaster SSH连接

要让你的Ruby项目里的Net::SSH复用已有的ControlMaster连接,避免重复MFA验证,只需调整Net::SSH.start的调用参数即可,有两种方式可选:

方式一:读取~/.ssh/config中的配置

如果你已经在~/.ssh/config里为host.example.com配置了ControlMaster相关选项(比如下面这样):

Host host.example.com
  ControlMaster auto
  ControlPath ~/.ssh/control-%r@%h:%p.sock
  ControlPersist 1h

只需在启动SSH会话时加上config: true参数,让Net::SSH加载并使用这些配置:

Net::SSH.start("host.example.com", nil, config: true) do |session|
  # ... 你的业务代码
end

方式二:手动指定连接复用参数

如果不想依赖~/.ssh/config,可以直接在代码里指定Control相关参数,格式和shell SSH保持一致即可:

Net::SSH.start("host.example.com", nil, 
  control_master: :auto,
  control_path: "~/.ssh/control-%r@%h:%p.sock",
  control_persist: "1h"
) do |session|
  # ... 你的业务代码
end

注意事项

  • 第一次建立连接时仍需完成MFA验证,后续连接会复用已建立的控制连接,无需重复验证
  • 确保ControlPath的格式和你使用shell SSH时的配置完全一致,这样才能复用同一个连接实例

内容的提问来源于stack exchange,提问作者Jellicle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 12:42:38