Channel

Abstraction for an SSH2 channel.

class paramiko.channel. Channel ( chanid )

A secure tunnel across an SSH Transport . A Channel is meant to behave like a socket, and has an API that should be indistinguishable from the Python socket API.

Because SSH2 has a windowing kind of flow control, if you stop reading data from a Channel and its buffer fills up, the server will be unable to send you any more data until you read some of it. (This won’t affect other channels on the same transport – all channels on a single transport are flow-controlled independently.) Similarly, if the server isn’t reading data you send, calls to send may block, unless you set a timeout. This is exactly like a normal network socket, so it shouldn’t be too surprising.

Instances of this class may be used as context managers.

__init__ ( chanid )

Create a new channel. The channel is not associated with any particular session or Transport until the Transport attaches it. Normally you would only call this method from the constructor of a subclass of Channel .

Parameters: chanid ( int ) – the ID of this channel, as passed by an existing Transport .
__repr__ ( )

Return a string representation of this object, for debugging.

active = None

Whether the connection is presently active

chanid = None

Channel ID

close ( )

Close the channel. All future read/write operations on the channel will fail. The remote end will receive no more data (after queued data is flushed). Channels are automatically closed when their Transport is closed or when they are garbage collected.

closed = None

Whether the connection has been closed

exec_command ( command )

Execute a command on the server. If the server allows it, the channel will then be directly connected to the stdin, stdout, and stderr of the command being executed.

When the command finishes executing, the channel will be closed and can’t be reused. You must open a new channel if you wish to execute another command.

Parameters: command ( str ) – a shell command to execute.
Raises: SSHException – if the request was rejected or the channel was closed
exit_status_ready ( )

Return true if the remote process has exited and returned an exit status. You may use this to poll the process status if you don’t want to block in recv_exit_status . Note that the server may not return an exit status in some cases (like bad servers).

Returns: True if recv_exit_status will return immediately, else False .

New in version 1.7.3.

fileno ( )

Returns an OS-level file descriptor which can be used for polling, but but not for reading or writing. This is primarily to allow Python’s select module to work.

The first time fileno is called on a channel, a pipe is created to simulate real OS-level file descriptor (FD) behavior. Because of this, two OS-level FDs are created, which will use up FDs faster than normal. (You won’t notice this effect unless you have hundreds of channels open at the same time.)

Returns: an OS-level file descriptor ( int )

Warning

This method causes channel reads to be slightly less efficient.

get_id ( )

Return the int ID # for this channel.

The channel ID is unique across a Transport and usually a small number. It’s also the number passed to ServerInterface.check_channel_request when determining whether to accept a channel request in server mode.

get_name ( )

Get the name of this channel that was previously set by set_name .

get_pty ( term='vt100' , width=80 , height=24 , width_pixels=0 , height_pixels=0 )

Request a pseudo-terminal from the server. This is usually used right after creating a client channel, to ask the server to provide some basic terminal semantics for a shell invoked with invoke_shell . It isn’t necessary (or desirable) to call this method if you’re going to execute a single command with exec_command .

Parameters:
  • term ( str ) – the terminal type to emulate (for example, 'vt100' )
  • width ( int ) – width (in characters) of the terminal screen
  • height ( int ) – height (in characters) of the terminal screen
  • width_pixels ( int ) – width (in pixels) of the terminal screen
  • height_pixels ( int ) – height (in pixels) of the terminal screen
Raises:

SSHException – if the request was rejected or the channel was closed

get_transport ( )

Return the Transport associated with this channel.

getpeername ( )

Return the address of the remote side of this Channel, if possible.

This simply wraps Transport.getpeername , used to provide enough of a socket-like interface to allow asyncore to work. (asyncore likes to call 'getpeername' .)

gettimeout ( )

Returns the timeout in seconds (as a float) associated with socket operations, or None if no timeout is set. This reflects the last call to setblocking or settimeout .

invoke_shell ( )

Request an interactive shell session on this channel. If the server allows it, the channel will then be directly connected to the stdin, stdout, and stderr of the shell.

Normally you would call get_pty before this, in which case the shell will operate through the pty, and the channel will be connected to the stdin and stdout of the pty.

When the shell exits, the channel will be closed and can’t be reused. You must open a new channel if you wish to open another shell.

Raises: SSHException – if the request was rejected or the channel was closed
invoke_subsystem ( subsystem )

Request a subsystem on the server (for example, sftp ). If the server allows it, the channel will then be directly connected to the requested subsystem.

When the subsystem finishes, the channel will be closed and can’t be reused.

Parameters: subsystem ( str ) – name of the subsystem being requested.
Raises: SSHException – if the request was rejected or the channel was closed
makefile ( *params )

Return a file-like object associated with this channel. The optional mode and bufsize arguments are interpreted the same way as by the built-in file() function in Python.

Returns: ChannelFile object which can be used for Python file I/O.
makefile_stderr ( *params )

Return a file-like object associated with this channel’s stderr stream. Only channels using exec_command or invoke_shell without a pty will ever have data on the stderr stream.

The optional mode and bufsize arguments are interpreted the same way as by the built-in file() function in Python. For a client, it only makes sense to open this file for reading. For a server, it only makes sense to open this file for writing.

Returns: ChannelStderrFile object which can be used for Python file I/O.

New in version 1.1.

makefile_stdin ( *params )

Return a file-like object associated with this channel’s stdin stream.

The optional mode and bufsize arguments are interpreted the same way as by the built-in file() function in Python. For a client, it only makes sense to open this file for writing. For a server, it only makes sense to open this file for reading.

Returns: ChannelStdinFile object which can be used for Python file I/O.

New in version 2.6.

recv ( nbytes )

Receive data from the channel. The return value is a string representing the data received. The maximum amount of data to be received at once is specified by nbytes . If a string of length zero is returned, the channel stream has closed.

Parameters: nbytes ( int ) – maximum number of bytes to read.
Returns: received data, as a str / bytes .
Raises: socket.timeout – if no data is ready before the timeout set by settimeout .
recv_exit_status ( )

Return the exit status from the process on the server. This is mostly useful for retrieving the results of an exec_command . If the command hasn’t finished yet, this method will wait until it does, or until the channel is closed. If no exit status is provided by the server, -1 is returned.

Warning

In some situations, receiving remote output larger than the current Transport or session’s window_size (e.g. that set by the default_window_size kwarg for Transport.__init__ ) will cause recv_exit_status to hang indefinitely if it is called prior to a sufficiently large Channel.recv (or if there are no threads calling Channel.recv in the background).

In these cases, ensuring that recv_exit_status is called after Channel.recv (or, again, using threads) can avoid the hang.

Returns: the exit code (as an int ) of the process on the server.

New in version 1.2.

recv_ready ( )

Returns true if data is buffered and ready to be read from this channel. A False result does not mean that the channel has closed; it means you may need to wait before more data arrives.

Returns: True if a recv call on this channel would immediately return at least one byte; False otherwise.
recv_stderr ( nbytes )

Receive data from the channel’s stderr stream. Only channels using exec_command or invoke_shell without a pty will ever have data on the stderr stream. The return value is a string representing the data received. The maximum amount of data to be received at once is specified by nbytes . If a string of length zero is returned, the channel stream has closed.

Parameters: nbytes ( int ) – maximum number of bytes to read.
Returns: received data as a str
Raises: socket.timeout – if no data is ready before the timeout set by settimeout .

New in version 1.1.

recv_stderr_ready ( )

Returns true if data is buffered and ready to be read from this channel’s stderr stream. Only channels using exec_command or invoke_shell without a pty will ever have data on the stderr stream.

Returns: True if a recv_stderr call on this channel would immediately return at least one byte; False otherwise.

New in version 1.1.

remote_chanid = None

Remote channel ID

request_forward_agent ( handler )

Request for a forward SSH Agent on this channel. This is only valid for an ssh-agent from OpenSSH !!!

Parameters: handler – a required callable handler to use for incoming SSH Agent connections
Returns: True if we are ok, else False (at that time we always return ok)
Raises: SSHException in case of channel problem.
request_x11 ( screen_number=0 , auth_protocol=None , auth_cookie=None , single_connection=False , handler=None )

Request an x11 session on this channel. If the server allows it, further x11 requests can be made from the server to the client, when an x11 application is run in a shell session.

From RFC 4254 :

It is RECOMMENDED that the 'x11 authentication cookie' that is
sent be a fake, random cookie, and that the cookie be checked and
replaced by the real cookie when a connection request is received.
													

If you omit the auth_cookie, a new secure random 128-bit value will be generated, used, and returned. You will need to use this value to verify incoming x11 requests and replace them with the actual local x11 cookie (which requires some knowledge of the x11 protocol).

If a handler is passed in, the handler is called from another thread whenever a new x11 connection arrives. The default handler queues up incoming x11 connections, which may be retrieved using Transport.accept . The handler’s calling signature is:

handler(channel: Channel, (address: str, port: int))