ssh2.channel

class ssh2.channel.Channel
close(self)

Close channel. Typically done to be able to get exit status.

eof(self)

Get channel EOF status.

Return type

bool

execute(self, command)

Execute command.

Parameters

command (str) – Command to execute

Raises

ssh2.exceptions.ChannelError on errors executing command

Return type

int

flush(self)

Flush stdout stream

flush_ex(self, int stream_id)

Flush stream with id

flush_stderr(self)

Flush stderr stream

get_exit_signal(self)

Get exit signal, message and language tag, if any, for command.

Returns (returncode`, exit signal, error message,

language tag) tuple.

Return type

tuple(int, bytes, bytes, bytes)

get_exit_status(self)

Get exit status of command.

Note that 0 is also failure code for this function.

Best used in non-blocking mode to avoid it being impossible to tell if 0 indicates failure or an actual exit status of 0

handle_extended_data(self, int ignore_mode)

Deprecated, use handle_extended_data2

handle_extended_data2(self, int ignore_mode)
ignore_extended_data(self, int ignore_mode)

Deprecated, use handle_extended_data2

poll_channel_read(self, int extended)

Deprecated - use session.block_directions and socket polling instead

process_startup(self, request, message=None)

Startup process on server for request with message.

Request is a supported SSH subsystem and clients would typically use one of execute/shell/subsystem functions depending on request type.

Parameters
  • request (str) – Request type (exec/shell/subsystem).

  • message (str or None) – Request message. Content depends on request type and can be None.

pty(self, term='vt100')

Request a PTY (physical terminal emulation) on the channel.

Parameters

term (str) – Terminal type to emulate.

read(self, size_t size=1024)

Read the stdout stream. Returns return code and output buffer tuple.

Return code is the size of the buffer when positive. Negative values are error codes.

Parameters

size (int) – Max buffer size to read.

Return type

(int, bytes)

read_ex(self, size_t size=1024, int stream_id=0)

Read the stream with given id. Returns return code and output buffer tuple.

Return code is the size of the buffer when positive. Negative values are error codes.

Parameters

size (int) – Max buffer size to read.

Return type

(int, bytes)

read_stderr(self, size_t size=1024)

Read the stderr stream. Returns return code and output buffer tuple.

Return code is the size of the buffer when positive. Negative values are error codes.

Return type

(int, bytes)

receive_window_adjust(self, unsigned long adjustment, unsigned long force)
receive_window_adjust2(self, unsigned long adjustment, unsigned long force)
send_eof(self)

Tell the remote host that no further data will be sent on the specified channel. Processes typically interpret this as a closed stdin descriptor.

Returns 0 on success or negative on failure. It returns LIBSSH2_ERROR_EAGAIN when it would otherwise block.

Return type

int

setenv(self, varname, value)

Set environment variable on channel.

Parameters
  • varname (str) – Name of variable to set.

  • value (str) – Value of variable.

Return type

int

shell(self)

Request interactive shell from channel.

Raises

ssh2.exceptions.ChannelError on errors requesting interactive shell.

subsystem(self, subsystem)

Request subsystem from channel.

Parameters

subsystem (str) – Name of subsystem

wait_closed(self)

Wait for server to acknowledge channel close command.

wait_eof(self)

Wait for the remote end to acknowledge an EOF request.

Returns 0 on success or negative on failure. It returns ssh2.error_codes.LIBSSH2_ERROR_EAGAIN when it would otherwise block.

Return type

int

window_read(self)
window_read_ex(self, unsigned long read_avail, unsigned long window_size_initial)
window_write(self)
window_write_ex(self, unsigned long window_size_initial)
write(self, buf)

Write buffer to stdin.

Returns tuple of (return_code, bytes_written).

In blocking mode bytes_written will always equal len(buf) if no errors have occurred which would raise exception.

In non-blocking mode return_code can be LIBSSH2_ERROR_EAGAIN and bytes_written can be less than len(buf).

Clients should resume from that point on next call to write, ie buf[bytes_written_in_last_call:].

Note

While this function handles unicode strings for buf argument, bytes_written offset will always be for the bytes representation thereof as returned by the C function calls which only handle byte strings.

Parameters

buf (str) – Buffer to write

Return type

tuple(int, int)

write_ex(self, int stream_id, buf)

Write buffer to specified stream id.

Returns tuple of (return_code, bytes_written).

In blocking mode bytes_written will always equal len(buf) if no errors have occurred which would raise exception.

In non-blocking mode return_code can be LIBSSH2_ERROR_EAGAIN and bytes_written can be less than len(buf).

Clients should resume from that point on next call to the function, ie buf[bytes_written_in_last_call:].

Note

While this function handles unicode strings for buf argument, bytes_written offset will always be for the bytes representation thereof as returned by the C function calls which only handle byte strings.

Parameters
  • stream_id (int) – Id of stream to write to

  • buf (str) – Buffer to write

Return type

tuple(int, int)

write_stderr(self, buf)

Write buffer to stderr.

Returns tuple of (return_code, bytes_written).

In blocking mode bytes_written will always equal len(buf) if no errors have occurred which would raise exception.

In non-blocking mode return_code can be LIBSSH2_ERROR_EAGAIN and bytes_written can be less than len(buf).

Clients should resume from that point on next call to write, ie buf[bytes_written_in_last_call:].

Note

While this function handles unicode strings for buf argument, bytes_written offset will always be for the bytes representation thereof as returned by the C function calls which only handle byte strings.

Parameters

buf (str) – Buffer to write

Return type

tuple(int, int)

x11_req(self, int screen_number)
x11_req_ex(self, int single_connection, const char *auth_proto, const char *auth_cookie, int screen_number)
session

Originating session.