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… =/~
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?
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.