开发者

How can I pipe data to a process started via Net::SSH on stdin?

I'm generating a data feed on the local machine, that I want to pipe into a remote process via Net::SSH.

Something like

echo foosball | sed 's/foo/bar/g'

Just that the echo foosball part would be the data feed on the local machine.

What I'm NOT looking for is:

data = "foosball"
s开发者_StackOverflow中文版sh.exec!("echo #{data} | sed 's/foo/bar/g'")

I really want a stream of data piped into the process in real time ;)


Okay, I figured it out:

#!/usr/bin/env ruby 

require 'rubygems'
require 'net/ssh'

res = ""
c = Net::SSH.start("127.0.0.1", "xxx", :password => "xxx")
c.open_channel do |channel|
  channel.exec("sed 's/foo/bar/g'") do |ch, success|
    channel.on_data do |ch,data|
      res << data
    end

    channel.send_data "foosball"
    channel.eof!
  end
end
c.loop
puts res # => "barsball"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜