Run command via SSH session displaying results via AJAX


#1

I am trying to create a web app that allows me to invoke a command on
another server via an SSH session. I am currently doing this as
follows:

  1. User is presented with a form to enter server, command, username and
    password.
  2. User clicks “Run” and form is submitted to the “run_command” action.
  3. The “run_command” action initiates an SSH session using net-ssh and
    registers callbacks with the SSH session to append any command output to
    the web session[:command_output]. Thus, session[:command_output] grows
    until all results (stdout and stderr) are accumulated.
  4. The “run_command” redirects to the results page which then renders
    the web session[:command_output] which is viewed by the user.

This is working fine, but what I really want to do is alter step 3 so
that the SSH session accumulates the results but the results page is
rendered before the SSH session is complete. Then in step 4 I could use
AJAX to periodically update the command output results as they come in
instead of waiting for them all to come in before displaying them.

In order to do this it seems I would need threading and that introduces
complexities and prefer to not deal with.

Another possibility I thought of was to somehow have the results page be
the one that makes an AJAX call to the “run_command” action that will
initiate the SSH session, and have the SSH session callbacks send the
data to the browser realtime instead of appending to the web session
variable. I’ve done something similar in Python with Twisted and
Nevow/LivePage before, but am unsure how to do so with Rails.

Does anyone have any thoughts or advice on how I might solve this
problem?

Thanks,
Justin


#2

What id you had one action that executed the command. This action
would do steps 1-3 below. Then have some AJAX on the view that
simply replaces the contents of the display div with the current
value of session[:command_output]. Since the session will persist
between calls, you will still have the entire output in the session.

Not sure if I’m on the right track here, but those are my initial
thoughts.

-Derrick S.


#3

Oh I think I know what you’re saying now.

If I have the form submitted via AJAX, then I don’t have to wait for the
next page to be displayed (the one that previously wasn’t being rendered
until the SSH session was done). So once they click the button to
submit, an AJAX call is made to run_command which appends output to the
session variable. In addition, clicking the buttom would have to start
a periodic refresh to display the contents of the session variable, thus
displaying the output as it accumulates.

Justin J. wrote:

Derrick S. wrote:

What id you had one action that executed the command. This action
would do steps 1-3 below. Then have some AJAX on the view that
simply replaces the contents of the display div with the current
value of session[:command_output]. Since the session will persist
between calls, you will still have the entire output in the session.

That is basically what I am talking about, except I’m trying to find a
way to start the SSH session and have it run in the background (another
thread?) and send its results to the page via AJAX calls.

Not sure if I’m on the right track here, but those are my initial
thoughts.

-Derrick S.


#4

Derrick S. wrote:

What id you had one action that executed the command. This action
would do steps 1-3 below. Then have some AJAX on the view that
simply replaces the contents of the display div with the current
value of session[:command_output]. Since the session will persist
between calls, you will still have the entire output in the session.

That is basically what I am talking about, except I’m trying to find a
way to start the SSH session and have it run in the background (another
thread?) and send its results to the page via AJAX calls.

Not sure if I’m on the right track here, but those are my initial
thoughts.

-Derrick S.