# Run command via SSH session displaying results via AJAX

**URL:** <https://www.ruby-forum.com/t/run-command-via-ssh-session-displaying-results-via-ajax/78545>\
**Category:** Rails\
**Created:** [March 8, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/run-command-via-ssh-session-displaying-results-via-ajax/78545 "2006-03-08T17:52:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [March 8, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/run-command-via-ssh-session-displaying-results-via-ajax/78545/1 "2006-03-08T17:52:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [March 8, 2006, 6:40pm UTC](https://www.ruby-forum.com/t/run-command-via-ssh-session-displaying-results-via-ajax/78545/2 "2006-03-08T18:40:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [March 8, 2006, 7:49pm UTC](https://www.ruby-forum.com/t/run-command-via-ssh-session-displaying-results-via-ajax/78545/3 "2006-03-08T19:49:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [March 8, 2006, 7:41pm UTC](https://www.ruby-forum.com/t/run-command-via-ssh-session-displaying-results-via-ajax/78545/4 "2006-03-08T19:41:17Z")

</div>

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.
