# Net/ssh & send\_data

**URL:** <https://www.ruby-forum.com/t/net-ssh-send-data/184634>\
**Category:** Ruby\
**Created:** [March 5, 2010, 2:43pm UTC](https://www.ruby-forum.com/t/net-ssh-send-data/184634 "2010-03-05T14:43:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dawa\_Ometto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ea924/32.png) [@Dawa\_Ometto](https://www.ruby-forum.com/u/Dawa_Ometto)\
**Post date:** [March 5, 2010, 2:43pm UTC](https://www.ruby-forum.com/t/net-ssh-send-data/184634/1 "2010-03-05T14:43:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dawa\_Ometto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ea924/32.png) [@Dawa\_Ometto](https://www.ruby-forum.com/u/Dawa_Ometto)\
**Post date:** [March 5, 2010, 11:37pm UTC](https://www.ruby-forum.com/t/net-ssh-send-data/184634/2 "2010-03-05T23:37:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Dawa\_Ometto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ea924/32.png) [@Dawa\_Ometto](https://www.ruby-forum.com/u/Dawa_Ometto)\
**Post date:** [April 11, 2010, 8:35pm UTC](https://www.ruby-forum.com/t/net-ssh-send-data/184634/3 "2010-04-11T20:35:01Z")

</div>

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/](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

---

<div class="post-metadata">

**Author:** ![Dawa\_Ometto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ea924/32.png) [@Dawa\_Ometto](https://www.ruby-forum.com/u/Dawa_Ometto)\
**Post date:** [April 12, 2010, 10:15pm UTC](https://www.ruby-forum.com/t/net-ssh-send-data/184634/4 "2010-04-12T22:15:31Z")

</div>

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.
