Net/ssh & send_data

Hi,

I would like to use the net/ssh gem to connect to an ssh server, execute
a command, and send data to the command’s standard input. So far I’ve
managed to do this using the on_data callback:

ch.exec “mycommand” do |ch, success|

ch.on_data do |ch, data| #Wait for invitation on std-out and send
message on std-in
output << data
if input != nil then
ch.send_data(input.gsub("\n","\r")+"\n") #Fix to prevent
multi line data from ending the data stream, append newline
end
end

end

However, this is slightly suboptimal - it means that the listening
script has to send something to standard out before I can send data to
it’s stdin. Does anyone have clue how this can be implemented without
using the on_data callback?

Kind regards,

Dawa

Dawa Ometto wrote:

However, this is slightly suboptimal - it means that the listening
script has to send something to standard out before I can send data to
it’s stdin. Does anyone have clue how this can be implemented without
using the on_data callback?

Not exactly answering your question, but Net::SSH::Telnet is a small
wrapper around Net::SSH which gives it an API much more similar to
Net::Telnet, which in turn is more of an expect-like interface.

On Mar 5, 2010, at 11:37 PM, Brian C. wrote:

Not exactly answering your question, but Net::SSH::Telnet is a small
wrapper around Net::SSH which gives it an API much more similar to
Net::Telnet, which in turn is more of an expect-like interface.

Thanks for the reply. Been a while since I managed to take a look at
this, but Net::SSH::Telnet doesn’t seem to be exactly what I want.
I’ve noticed that in the net/ssh version < 2, a ‘popen3’ command is
implemented that allows me to do exactly what I want, as e.g. here:

http://codeidol.com/other/rubyckbk/Internet-Services/Being-an-SSH-Client/

The magic is in session.process.popen3(‘command’), when ‘session’ is a
running net::ssh session. However, session.process returns something
entirely different in net:ssh v2 and up, and the popen3 method doesn’t
seem to be implemented at all. This essentially looks like a downgrade
to me, or is there another way to get something like popen3 working?
If not, would it be safe to use a pre-2.0 version of net-ssh?

Cheers,

Dawa Ometto

Dawa Ometto wrote:

If not, would it be safe to use a pre-2.0 version of net-ssh?

Depends what you mean by “safe”. I don’t know if Net::SSH 1.x is being
maintained; it may not work with ruby 1.9 for example. But it works fine
with 1.8.x and I’m not aware of any known security issues.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs