2

I'm in ruby on rails and using the net-ssh gem to ssh into another machine, run some scripts, get the output of the scripts back into rails, then serve that output on my site. This will need to happen a lot, in fact it is the core of my site.

The script itself takes very little time, but the time to response ranges between instant to 15 seconds. The SSH handshake is taking variable time.

Here's my ruby code to do the ssh:

res = []
Net::SSH.start( host, user, :password => pass ) do |ssh|
  # execute container
  ssh.exec("run script") do |ch, stream, data|
      res.push(data)
  end
end
res = res.join("")
render text: res

Any idea for how to make this super fast?

I'm wondering if I should be keeping an ssh connection open at all times and add new threads every time I need to run the script (which will be a lot). I've seen people talk about ControlMaster, but not sure how to make that work.

bumpkin
  • 3,321
  • 4
  • 25
  • 32
  • Do you get the same occasional slow down when using command line `ssh` from your app server to the ssh server? Is the network between them reliable? – Matt Jul 12 '14 at 06:55

1 Answers1

0

If this is the core of your site, you may not want to rely on SSH and exec.

The alternative is to use Ruby/Rails instead of SSH on the external host and push the jobs directly to Ruby via a more standard web API or a messaging system like zeromq. Creating a consistent Service-oriented architecture.

Otherwise, you should maintain the SSH connection from Ruby as a long running task in a background daemon then push jobs to it via IPC (like zeromq or http or simply your database) then wait for the response. Then you only have the overhead of starting up your scripts on the remote host for each request.

Matt
  • 68,711
  • 7
  • 155
  • 158