如何将数据传输到在stdin上通过Net :: SSH启动的进程?

fli*_*ald 10 ruby ssh

我正在本地机器上生成数据馈送,我想通过Net :: SSH管道进入远程进程.

就像是

echo foosball | sed 's/foo/bar/g'
Run Code Online (Sandbox Code Playgroud)

只是该echo foosball部分将是本地机器上的数据馈送.

想要的是:

data = "foosball"
ssh.exec!("echo #{data} | sed 's/foo/bar/g'")
Run Code Online (Sandbox Code Playgroud)

我真的想要实时传输到流程中的数据流;)

fli*_*ald 8

好的,我想通了:

#!/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"
Run Code Online (Sandbox Code Playgroud)