Ruby Net:SSH控制大师? [英] Ruby Net:SSH Control Master?

查看:143
本文介绍了Ruby Net:SSH控制大师?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个Ruby(Rails)应用程序,该应用程序需要建立许多短的SSH连接.使用Ruby 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.

是否可以通过Ruby Net :: SSH启用Control Master?在命令行上进行测试时,由于连接已经打开(协商了密钥等),因此使登录(在第一个登录之后)非常快.

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.).

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

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.

谢谢!

推荐答案

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

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

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

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.

是您想要的吗?

这篇关于Ruby Net:SSH控制大师?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆