Method: Net::SSH::Connection::Session#listen_to

Defined in:
lib/net/ssh/connection/session.rb

#listen_to(io, &callback) ⇒ Object

Adds an IO object for the event loop to listen to. If a callback is given, it will be invoked when the io is ready to be read, otherwise, the io will merely have its #fill method invoked.

Any io value passed to this method must have mixed into it the Net::SSH::BufferedIo functionality, typically by calling #extend on the object.

The following example executes a process on the remote server, opens a socket to somewhere, and then pipes data from that socket to the remote process’ stdin stream:

channel = ssh.open_channel do |ch|
  ch.exec "/some/process/that/wants/input" do |ch, success|
    abort "can't execute!" unless success

    io = TCPSocket.new(somewhere, port)
    io.extend(Net::SSH::BufferedIo)
    ssh.listen_to(io)

    ch.on_process do
      if io.available > 0
        ch.send_data(io.read_available)
      end
    end

    ch.on_close do
      ssh.stop_listening_to(io)
      io.close
    end
  end
end

channel.wait


481
482
483
# File 'lib/net/ssh/connection/session.rb', line 481

def listen_to(io, &callback)
  listeners[io] = callback
end