3. Channels
3.1. What are Channels?
The SSH protocol requires that requests for services on a remote machine be made over channels. A single SSH connection may contain multiple channels, all run simultaneously over that connection.
Each channel, in turn, represents the processing of a single service. When you invoke a process on the remote host with Net::SSH, a channel is opened for that invocation, and all input and output relevant to that process is sent through that channel. The connection itself simply manages the packets of all of the channels that it has open.
This means that, for instance, over a single SSH connection you could execute a process, download a file via SFTP, and forward any number of ports, all (seemingly) at the same time!
Naturally, they do not occur simultaneously, but rather work in a “time-share” fashion by sharing the bandwidth of the connection. Nevertheless, the fact that these channels exist make working with the SSH protocol a bit more challenging than simpler protocols (like FTP, HTTP, or Telnet).
3.2. Session.loop
Because a session may be composed of multiple simultaneously operating channels, the Net::SSH interface works by means of callbacks. You specify actions that need to occur in response to various events, and when those events occur, the framework invokes the corresonding callbacks.
In order to allow the events to be processed in a continuous manner, you need to be sure to call the loop
method of your session handle, after setting up any callbacks that you want to be executed. If you do not call the loop
method, your session will terminate as soon as the block is exited, which means none of your carefully laid callbacks will ever be called.
The loop
method is easy to invoke:
1 2 3 4 | Net::SSH.start( 'host' ) do |session| ... session.loop end |
Incidentally, the loop
method accepts an optional block, which if specified should return a “false” value when the loop should terminate. In the absense of a block, the loop will continue until there are no more open channels. Sometimes, however, you only want the loop to continue until some action occurs, at which time you then do some processing and then start the loop again.
3.3. Channel Types
Each channel has a type. Usually, you will use “session” channels, but there are also “x11” channels, “forwarded-tcpip” channels, and “direct-tcpip” channels. Net::SSH currently has no support for “x11” channels. The “forwarded-tcpip” and “direct-tcpip” channels are managed internally via the port-forwarding interfaces.
The “session” channel type allows for a broad range of actions, including (but not limited to) SFTP requests and remote process execution.
3.4. Opening a Channel
The simplest way to open a channel is via the open_channel
method of Net::SSH::Session. By default, the channel will be of type “session”, but you can optionally specify the channel type and any extra data to initialize the channel with. You also pass a block to the open_channel
invocation. This block will be called after the server has confirmed that the channel is valid and has been opened successfully.
The open_channel
method always returns immediately—all it does is inform the server that a channel needs to be opened and then registers the associated block as the callback to be invoked when the channel is confirmed.
This behavior is typical of most of the methods in the Net::SSH API; they simply send a request to the server and then (optionally) register a callback. Very few of them actually block (pause) until the server responds.
Here is an example of opening a channel:
1 2 3 4 5 6 7 8 | Net::SSH.start( 'host' ) do |session| session.open_channel do |channel| puts "channel successfully opened... closing..." channel.close end session.loop end |
Note the use of the close
method for the channel. Just like most methods in the Net::SSH API, it does not immediately close the channel, but instead sends a close request to the server and returns. When the server responds that the channel has been closed, the framework will then call any final callbacks for the channel and then remove it.
3.5. Callbacks
There are various callbacks that may be registered on a channel. Registering a callback is as simple as invoking the corresponding method on the channel and giving it a block that should be invoked when the named action occurs. The following table describes each callback and how it is used.
Name | Description |
---|---|
on_close |
This callback should accept a single parameter: the channel being closed. It is called immediately after the channel is removed from the connection. The callback should not send any data through the channel, since at this point the channel is no longer connected to the remote host. |
on_confirm_failed |
This callback should accept four parameters: the channel instance, the reason code, a description of why the confirm failed, and the language code for the message. It is called immediately after the server has indicated that a channel could not be opened. |
on_confirm_open |
This callback should accept a single parameter: the channel being opened. It is called immediately after the server has confirmed that the channel has been opened. This callback is typically set by a block passed to an #open_channel call. |
on_data |
This callback is invoked when data is received over the channel from the remote server. This data typically corresponds to the remote process’s stdout stream. The channel should accept two parameters: the channel itself, and the data being received. |
on_eof |
This callback is called when the server indicates that no more data will be sent from the server over this channel. Your program is still welcome to send data to the server, but you are guaranteed at this point that your on_data and on_extended_data callbacks will no longer be called for this channel. The callback should accept a single parameter, the channel itself. |
on_extended_data |
This callback is called when extended data is received from the server. There are (potentially) many types of extended data. The callback should accept three parameters: the channel, an integer indicating the type of the data, and the data itself. Right now, you can pretty much count on the data type being a “1”, which corresponds to the remote process’s stderr stream. Other data types are not defined in the SSH specification, but that does not mean some SSH servers won’t try to invent their own. |
on_failure |
When a request is sent over a channel (via the send_request or send_request_string methods), it may either succeed or fail. If it fails, this callback will be invoked. It should take a single parameter: the channel itself. |
on_request |
When the server sends a “channel request” to the client, this callback will be invoked. Channel requests from the server typically indicate things like the exit status of a process. This callback should take four parameters: the channel, the type of request (as a string, like “exit-status”), a boolean (indicating whether or not the server wants an explicit reply to this request), and the data from the request, which will be a buffer object (see the API documentation for Net::SSH::Util::ReaderBufferImpl ). |
on_success |
When a request is sent over a channel (via the send_request or send_request_string methods), it may either succeed or fail. If it succeeds, this callback will be invoked. It should take a single parameter: the channel itself. |
on_window_adjust |
When the server asks the client to adjust this channel’s window size, this callback will be invoked. It should accept two parameters: the channel, and the number of bytes to add the channel’s window size. |
In general, you will never need to register callbacks for on_failure
, on_request
, on_success
, or on_window_adjust
, unless you are needing to implement support for some subservice or piggy-backed protocol (like SFTP).
Following is an example of registering callbacks on a channel:
1 2 3 4 5 6 7 8 9 10 11 | Net::SSH.start( 'host' ) do |session| session.open_channel do |channel| channel.on_close do |ch| puts "channel closed successfully." end puts "closing channel..." channel.close end session.loop end |
3.6. Channel Operations
There are a variety of operations that may be performed on a channel. Some we’ve already mentioned, like registering callbacks, or closing the channel. Some of the other more common operations are listed (and described) in the following table.
Operation | Description |
---|---|
#exec |
Executes a command asynchronously on this channel. |
#request_pty |
Requests that a pseudo-terminal (pty) be opened for this channel. |
#send_data |
Sends the given data string to the server via this channel. This is useful for sending data to a remote process, or sending an SFTP packet to the SFTP subsystem. |
#send_eof |
Tells the server that no further data will be sent from the client to the server. The client must honor this by not sending any more data (either normal or extended) to the server over this channel. |
#send_extended_data |
Sends a data string to the server, along with an integer describing its type. This is typically used to send stderr data. |
#send_request |
Sends a named request to the server for this channel. This is primarily used by implementations of protocols and subsystems that run on top of SSH. |
#send_signal |
Indicates that the server should send the given signal to the process on the other end of the channel. |
#subsystem |
Requests that the server start the given subsystem on this channel. This is how (for instance) the SFTP subsystem is invoked. |
See the API documentation for an exhaustive reference of all available channel operations.