Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Net:SSH Control Master?

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!

like image 589
Harry Avatar asked Jan 09 '12 11:01

Harry


2 Answers

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 ?

like image 112
Schmurfy Avatar answered Oct 11 '22 16:10

Schmurfy


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?

like image 43
sunkencity Avatar answered Oct 11 '22 16:10

sunkencity