# Working with a command-line application on a remote server via Net::SSH

**URL:** <https://www.ruby-forum.com/t/working-with-a-command-line-application-on-a-remote-server-via-net-ssh/180203>\
**Category:** Ruby\
**Created:** [December 14, 2009, 6:30pm UTC](https://www.ruby-forum.com/t/working-with-a-command-line-application-on-a-remote-server-via-net-ssh/180203 "2009-12-14T18:30:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oddly\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f04885/32.png) [@Oddly\_Z](https://www.ruby-forum.com/u/Oddly_Z)\
**Post date:** [December 14, 2009, 6:30pm UTC](https://www.ruby-forum.com/t/working-with-a-command-line-application-on-a-remote-server-via-net-ssh/180203/1 "2009-12-14T18:30:22Z")

</div>

Hello, all–

I am trying to interact with a command-line application on a remote  
machine using Net::SSH and trying to figure out how to send data to  
the application after invoking it. My code is somewhat like this:

> > Net::SSH.start(ip\_address, user, :password =\> password) do |ssh|  
> > ?\> ssh.exec ‘./solve -iy -c /path/to/config/file’ do |ch, stream,  
> > data|  
> > ?\> if stream == :stderror
> 
> > puts “ERROR: #{data}”  
> > else  
> > ?\> puts data
> 
> > …  
> > end  
> > end  
> > end  
> > environment variable PV\_VRESULTS\_LIMIT not set  
> > bb  
> > bd  
> > bh  
> > br  
> > ch  
> > fb  
> > fl  
> > ja  
> > jh  
> > li  
> > ma  
> > mm  
> > nn  
> > op  
> > pp  
> > ps  
> > rh  
> > rp  
> > rw  
> > sb  
> > wb  
> > …

Now, at this point, I need to send ‘li’ to the server to select which  
case to run. After that, the command-line application will query me  
with other options that I need to respond to and send across the wire.  
My question is this:

How do I send the data once the application is invoked on the remote  
machine, wait for the response (checking that against possible  
responses), then send more data, and repeat until all the options are  
sent, until I get the final output from the application?

Any help is greatly appreciated, as my head is starting to bleed from  
banging it against my desk for the past couple hours… =/~

–Mark

---

<div class="post-metadata">

**Author:** ![Oddly\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f04885/32.png) [@Oddly\_Z](https://www.ruby-forum.com/u/Oddly_Z)\
**Post date:** [December 14, 2009, 9:21pm UTC](https://www.ruby-forum.com/t/working-with-a-command-line-application-on-a-remote-server-via-net-ssh/180203/2 "2009-12-14T21:21:53Z")

</div>

On Dec 14, 12:23 pm, Oddly Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How do I send the data once the application is invoked on the remote  
> machine, wait for the response (checking that against possible  
> responses), then send more data, and repeat until all the options are  
> sent, until I get the final output from the application?

Well, I’ll answer my own question for historical purposes. I found  
that Net::SSH::Connection::Session#open\_channel to do exactly what I  
need. I can then pass in a block and use the #on\_data callback to  
interact with my remote command-line application. The code now looks  
somewhat like:

```
Net::SSH.start(@server, @user, :password => @pass) do |ssh|
  ssh.open_channel do |channel|
    channel.exec "./solve -iy -c /path/to/config/file" do |ch,

```

success|  
abort “Could not execute command!” unless success  
channel.on\_data do |ch, data|  
puts “got stdout: #{data}”  
channel.send\_data “li\n”  
puts “got stdout: #{data}”  
channel.send\_data “etc.\n”  
…  
end  
end  
end  
#ssh.loop – bleck!  
end

I am sure that I can clean this up now by queueing up the data to be  
sent and waiting on the response rather than just cycling through all  
this in a loop like it does now. Any suggestions for this pattern?
