Ruby Net:SSH控制大师?

发布于 2024-12-25 10:42:52 字数 292 浏览 1 评论 0原文

我目前有一个 Ruby (Rails) 应用程序,需要建立大量的短 SSH 连接。使用 Ruby Net::SSH 库可以正常工作,只是每次我想发出命令时应用程序都必须登录并协商密钥,这太慢了。

有没有办法使用 Ruby Net::SSH 启用 Control Master?在命令行测试中,这使得登录(第一次登录后)非常快,因为连接已经打开(密钥已协商等)。

如果 Net::SSH 无法做到这一点,有人可以建议一个可以做到这一点的替代库吗?

我想这一定是一个常见的要求,所以希望有人能提供帮助。

谢谢!

I currently have a Ruby (Rails) application that needs to make a lot of short SSH connections. This works fine using the Ruby Net::SSH library, except that the app has to log in and negotiate keys every time I want to make a command, which is too slow.

Is there a way to enable Control Master with Ruby Net::SSH? In testing on the command line, this makes logins (after the first one) very fast, since the connection is already open (keys are negotiated etc.).

If there is no way to do this with Net::SSH, can anybody suggest an alternative library that could do it?

I imagine this must be a common requirement, so hopefully someone can help.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮光沉寂 2025-01-01 10:42:52

为什么不保持连接打开呢?
ssh 调用是虚拟的,因为我不知道 api,但它达到了其目的:

def ssh_execute(user, command)
  Thread.current[:user_connections] ||= {}

  unless Thread.current[:user_connections][user.id]
    Thread.current[:user_connections][user.id] = Net::SSH.connect(...)
  end

  ssh = Thread.current[:user_connections][user.id]
  ssh.run_command(command)
end

每个线程将获得一个 ssh 连接,或者如果您的应用程序与乘客一起部署,则每个进程将有一个连接并将重用它。

是你想要的吗?

Why not just keeping the connection open ?
The ssh calls are dummy as I don't know the api but it serves its purpose:

def ssh_execute(user, command)
  Thread.current[:user_connections] ||= {}

  unless Thread.current[:user_connections][user.id]
    Thread.current[:user_connections][user.id] = Net::SSH.connect(...)
  end

  ssh = Thread.current[:user_connections][user.id]
  ssh.run_command(command)
end

You will get one ssh connection per thread or if your application is deployed with passenger each process will have one connection and will reuse it.

Is it what you want ?

甜嗑 2025-01-01 10:42:52

您可以准确指定您想要哪种加密协议来最大程度地减少握手,但是,是的,如果您需要扩展它,SSH 可能会有点慢。

一个非常有趣的做法是 http://saltstack.org/ 它在顶部实现了自己的“ssh”替代方案0mq,使得在多个服务器上运行并行命令变得超快,显然不会遇到与 ssh 相关的性能问题(例如厨师等)的相同问题。也许您可以在 salt 的基础上这样做吗?

You can specify exactly what kind of encryption protocol you want as to minimize the handshake, but well, yeah SSH can be a bit slow if you need to scale it up.

A really interesting take on this is http://saltstack.org/ it has implemented it's own "ssh" alternative on top of 0mq, making it super-fast to run parallel commands on multiple servers, apparently not having the same problems of ssh-related performance issues such as with chef etc. Maybe you could build upon salt do do this?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文