class Net::SSH::Connection::Channel

The channel abstraction. Multiple “channels” can be multiplexed onto a single SSH channel, each operating independently and seemingly in parallel. This class represents a single such channel. Most operations performed with the Net::SSH library will involve using one or more channels.

Channels are intended to be used asynchronously. You request that one be opened (via Connection::Session#open_channel), and when it is opened, your callback is invoked. Then, you set various other callbacks on the newly opened channel, which are called in response to the corresponding events. Programming with Net::SSH works best if you think of your programs as state machines. Complex programs are best implemented as objects that wrap a channel. See Net::SCP and Net::SFTP for examples of how complex state machines can be built on top of the SSH protocol.

ssh.open_channel do |channel|
  channel.exec("/invoke/some/command") do |ch, success|
    abort "could not execute command" unless success

    channel.on_data do |ch, data|
      puts "got stdout: #{data}"
      channel.send_data "something for stdin\n"
    end

    channel.on_extended_data do |ch, type, data|
      puts "got stderr: #{data}"
    end

    channel.on_close do |ch|
      puts "channel is closing!"
    end
  end
end

ssh.loop

Channels also have a basic hash-like interface, that allows programs to store arbitrary state information on a channel object. This helps simplify the writing of state machines, especially when you may be juggling multiple open channels at the same time.

Note that data sent across SSH channels are governed by maximum packet sizes and maximum window sizes. These details are managed internally by Net::SSH::Connection::Channel, so you may remain blissfully ignorant if you so desire, but you can always inspect the current maximums, as well as the remaining window size, using the reader attributes for those values.