Net/ssh & send_data

D

Dawa Ometto

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
 
B

Brian Candler

Dawa said:
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.
 
D

Dawa Ometto

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
 
B

Brian Candler

Dawa said:
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.
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top