Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ssh

ruby

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"
ssh.exec!("echo #{data} | sed 's/foo/bar/g'")

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

like image 638
flitzwald Avatar asked Sep 12 '10 14:09

flitzwald


1 Answers

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"
like image 189
flitzwald Avatar answered Nov 15 '22 18:11

flitzwald