Working with a command-line application on a remote server via

O

Oddly Zen

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:
?> ssh.exec './solve -iy -c /path/to/config/file' do |ch, stream,
data|
?> if stream == :stderrorenvironment 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
 
O

Oddly Zen

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top