Qore SSH2 Module  1.3
Qore::SSH2::SSH2Channel Class Reference

allows Qore programs to send and receive data through an ssh2 channel More...

Public Member Functions

nothing close (timeout timeout=-1)
 Closes the channel. More...
 
 constructor ()
 Throws an exception; the constructor cannot be called manually. More...
 
 copy ()
 Throws an exception; currently SSH2Channel objects cannot be copied. More...
 
 destructor ()
 destroys the object by disconnecting the channel and deregistering with the parent object
 
bool eof ()
 returns True if an end of file condition is flagged on an open channel More...
 
nothing exec (string command, timeout timeout=-1)
 Executes a command on the channel. More...
 
nothing extendedDataIgnore (timeout timeout=-1)
 Discard all data in substreams (i.e. stderr, etc) immediately when it arrives. More...
 
nothing extendedDataMerge (timeout timeout=-1)
 Merge substreams (i.e. stderr, etc) into stream 0 for reading. More...
 
nothing extendedDataNormal (timeout timeout=-1)
 Queue data in substreams (i.e. stderr, etc) for eventual reading. More...
 
string getEncoding ()
 Returns the name of the default string encoding of the channel. More...
 
int getExitStatus ()
 Returns the exit code raised by the process running on the remote host at the other end of the named channel. More...
 
string read (softint stream_id=0, timeout timeout=10000)
 Reads data on the given stream and returns it as a string. More...
 
binary readBinary (softint stream_id=0, timeout timeout=10000)
 Reads data on the given stream and returns it as a binary object. More...
 
binary readBinaryBlock (softint size, softint stream_id=0, timeout timeout=-1)
 Reads a block of data of a given size on the given stream and returns it as a binary object. More...
 
string readBlock (softint size, softint stream_id=0, timeout timeout=-1)
 Reads a block of data of a given size on the given stream and returns it as a string. More...
 
nothing requestPty (string term="vanilla", string modes="", softint width=LIBSSH2_TERM_WIDTH, softint height=LIBSSH2_TERM_HEIGHT, softint width_px=LIBSSH2_TERM_WIDTH_PX, softint height_px=LIBSSH2_TERM_HEIGHT_PX, timeout timeout=-1)
 Requests a terminal to be allocated to the channel. More...
 
nothing requestX11Forwarding (softint screen_no=0, bool single_connection=False, string auth_proto="", string auth_cookie="", timeout timeout=-1)
 Request X11 forwarding on the channel. More...
 
nothing sendEof (timeout timeout=-1)
 Informs the remote end that we are closing the connection. More...
 
nothing setEncoding (string encoding)
 Sets the default string encoding for the channel; all string data read from the channel will be tagged with the encoding given. More...
 
nothing setenv (string var, string value, timeout timeout=-1)
 Sets an environment variable on the channel. More...
 
nothing shell (timeout timeout=-1)
 Request a login shell to be started for the channel. More...
 
nothing subsystem (string command, timeout timeout=-1)
 Executes a command on the channel as a subsystem. More...
 
nothing waitClosed (timeout timeout=-1)
 Wait for the remote end to acknowledge the close request. More...
 
nothing waitEof (timeout timeout=-1)
 Wait for the remote end to acknowledge an EOF request. More...
 
nothing write (data data, softint stream_id=0, timeout timeout_ms=-1)
 Writes data to a stream. More...
 

Detailed Description

allows Qore programs to send and receive data through an ssh2 channel

Restrictions:
Qore::PO_NO_NETWORK

Member Function Documentation

◆ close()

nothing Qore::SSH2::SSH2Channel::close ( timeout  timeout = -1)

Closes the channel.

Example:
chan.close(30s);
Parameters
timeoutan integer giving a timeout in milliseconds or a relative date/time value (ex: 15s for 15 seconds); a negative value means do not time out
Exceptions
SSH2CHANNEL-CLOSE-ERRORserver returned an error while waiting for a response
SSH2CHANNEL-ERRORthe channel has been closed
SSH2CHANNEL-TIMEOUTtimeout communicating on channel
SSH2-ERRORsocket error sending data; timeout on socket; invalid SSH2 protocol response; server returned an error message

◆ constructor()

Qore::SSH2::SSH2Channel::constructor ( )

Throws an exception; the constructor cannot be called manually.

Throws an exception if called directly; this class cannot be instantiated directly

Exceptions
SSH2CHANNEL-CONSTRUCTOR-ERRORthis class cannot be directly constructed but is created from methods in the SSH2Client class

◆ copy()

Qore::SSH2::SSH2Channel::copy ( )

Throws an exception; currently SSH2Channel objects cannot be copied.

Exceptions
SSH2CHANNEL-COPY-ERRORcopying SSH2Channel objects is not supported

◆ eof()

bool Qore::SSH2::SSH2Channel::eof ( )

returns True if an end of file condition is flagged on an open channel

Example:
bool b = chan.eof();
Returns
True if an end of file condition is flagged on an open channel, False if not
Exceptions
SSH2CHANNEL-ERRORthe channel has been closed

◆ exec()

nothing Qore::SSH2::SSH2Channel::exec ( string  command,
timeout  timeout = -1 
)

Executes a command on the channel.

Example:
chan.exec("ls -l", 30s);

use SSH2Channel::read() to read the output

Parameters
commandthe command to run
timeoutan integer giving a timeout in milliseconds or a relative date/time value (ex: 15s for 15 seconds); a negative value means do not time out
Exceptions
SSH2CHANNEL-EXEC-ERRORlibssh2 reported an error on the channel while waiting for confirmation from the server
SSH2CHANNEL-ERRORthe channel has been closed
SSH2CHANNEL-TIMEOUTtimeout communicating on channel
SSH2-ERRORsocket error sending data; timeout on socket; invalid SSH2 protocol response; server returned an error message

◆ extendedDataIgnore()

nothing Qore::SSH2::SSH2Channel::extendedDataIgnore ( timeout  timeout = -1)

Discard all data in substreams (i.e. stderr, etc) immediately when it arrives.

Example:
chan.extendedDataIgnore(30s);
Parameters
timeoutan integer giving a timeout in milliseconds or a relative date/time value (ex: 15s for 15 seconds); a negative value means do not time out
Exceptions
SSH2CHANNEL-EXTENDEDDATAIGNORE-ERRORserver returned an error while waiting for a response
SSH2CHANNEL-ERRORthe channel has been closed
SSH2CHANNEL-TIMEOUTtimeout communicating on channel
SSH2-ERRORsocket error sending data; timeout on socket; invalid SSH2 protocol response; server returned an error message

◆ extendedDataMerge()

nothing Qore::SSH2::SSH2Channel::extendedDataMerge ( timeout  timeout = -1)

Merge substreams (i.e. stderr, etc) into stream 0 for reading.

Example:
chan.extendedDataMerge(30s);
Parameters
timeoutan integer giving a timeout in milliseconds or a relative date/time value (ex: 15s for 15 seconds); a negative value means do not time out
Exceptions
SSH2CHANNEL-EXTENDEDDATAMERGE-ERRORserver returned an error while waiting for a response
SSH2CHANNEL-ERRORthe channel has been closed
SSH2CHANNEL-TIMEOUTtimeout communicating on channel
SSH2-ERRORsocket error sending data; timeout on socket; invalid SSH2 protocol response; server returned an error message

◆ extendedDataNormal()

nothing Qore::SSH2::SSH2Channel::extendedDataNormal ( timeout  timeout = -1)

Queue data in substreams (i.e. stderr, etc) for eventual reading.

Example:
chan.extendedDataNormal(30s);
Parameters
timeoutan integer giving a timeout in milliseconds or a relative date/time value (ex: 15s for 15 seconds); a negative value means do not time out
Exceptions
SSH2CHANNEL-EXTENDEDDATANORMAL-ERRORserver returned an error while waiting for a response
SSH2CHANNEL-ERRORthe channel has been closed
SSH2CHANNEL-TIMEOUTtimeout communicating on channel
SSH2-ERRORsocket error sending data; timeout on socket; invalid SSH2 protocol response; server returned an error message

◆ getEncoding()

string Qore::SSH2::SSH2Channel::getEncoding ( )

Returns the name of the default string encoding of the channel.

Code Flags:
CONSTANT
Example:
string enc = chan.getEncoding();
Returns
the name of the default string encoding of the channel

◆ getExitStatus()

int Qore::SSH2::SSH2Channel::getExitStatus ( )

Returns the exit code raised by the process running on the remote host at the other end of the named channel.

Example:
int rc = chan.getExitStatus();

Note that the exit status may not be available if the remote end has not yet set its status to closed

Returns
the exit code raised by the process running on the remote host at the other end of the named channel
Exceptions
SSH2CHANNEL-ERRORthe channel has been closed

◆ read()

string Qore::SSH2::SSH2Channel::read ( softint  stream_id = 0,
timeout  timeout = 10000 
)

Reads data on the given stream and returns it as a string.

Example:
string str = chan.read(0, 30s);
Parameters
stream_idthe stream ID to read (0 is the default, meaning stdin, 1 is for stderr
timeoutan integer giving a timeout in milliseconds or a relative date/time value (ex: 15s for 15 seconds); a negative value means do not time out
Exceptions
SSH2CHANNEL-READ-ERRORexpecting non-negative integer for stream id as optional first argument to SSH2Channel::read(); use 0 for stdin, 1 for stderr; libssh2 reported an error on the channel while waiting for data from the server
SSH2CHANNEL-ERRORthe channel has been closed
SSH2CHANNEL-TIMEOUTtimeout communicating on channel
SSH2-ERRORsocket error sending data; timeout on socket; invalid SSH2 protocol response; server returned an error message

◆ readBinary()

binary Qore::SSH2::SSH2Channel::readBinary ( softint  stream_id = 0,
timeout  timeout = 10000 
)

Reads data on the given stream and returns it as a binary object.

Example:
string str = chan.read(0, 30s);
Parameters
stream_idthe stream ID to read (0 is the default, meaning stdin, 1 is for stderr
timeoutan integer giving a timeout in milliseconds or a relative date/time value (ex: 15s for 15 seconds); a negative value means do not time out
Exceptions
SSH2CHANNEL-READBINARY-ERRORexpecting non-negative integer for stream id as optional first argument to SSH2Channel::read(); use 0 for stdin, 1 for stderr; libssh2 reported an error on the channel while waiting for data from the server
SSH2CHANNEL-ERRORthe channel has been closed
SSH2CHANNEL-TIMEOUTtimeout communicating on channel
SSH2-ERRORsocket error sending data; timeout on socket; invalid SSH2 protocol response; server returned an error message

◆ readBinaryBlock()

binary Qore::SSH2::SSH2Channel::readBinaryBlock ( softint  size,
softint  stream_id = 0,
timeout  timeout = -1 
)

Reads a block of data of a given size on the given stream and returns it as a binary object.

Example:
binary b = chan.readBinaryBlock(4096, 0, 30s);
Parameters
sizethe maximum size of the block of data to read in bytes
stream_idthe stream ID to read (0 is the default, meaning stdin, 1 is for stderr
timeoutan integer giving a timeout in milliseconds or a relative date/time value (ex: 15s for 15 seconds); a negative value means do not time out
Returns
a binary object of data representing the block read
Exceptions
SSH2CHANNEL-READBLOCK-ERRORzero or negative value passed for block size; negative value passed for stream id; libssh2 reported an error on the channel while waiting for data from the server
SSH2CHANNEL-ERRORthe channel has been closed
SSH2CHANNEL-TIMEOUTtimeout communicating on channel
SSH2-ERRORsocket error sending data; timeout on socket; invalid SSH2 protocol response; server returned an error message

◆ readBlock()

string Qore::SSH2::SSH2Channel::readBlock ( softint  size,
softint  stream_id = 0,
timeout  timeout = -1 
)

Reads a block of data of a given size on the given stream and returns it as a string.

Example:
string str = chan.readBlock(4096, 0, 30s);
Parameters
sizethe maximum size of the block of data to read in bytes
stream_idthe stream ID to read (0 is the default, meaning stdin, 1 is for stderr
timeoutan integer giving a timeout in milliseconds or a relative date/time value (ex: 15s for 15 seconds); a negative value means do not time out
Returns
a string representing the block read; note that if a multibyte character encoding is being read and the end of the block lands in the middle of a character, then the string returned (or the next string read) could have invalid bytes if an attempt is made to use the string as-is
Exceptions
SSH2CHANNEL-READBLOCK-ERRORzero or negative value passed for block size; negative value passed for stream id; libssh2 reported an error on the channel while waiting for data from the server
SSH2CHANNEL-ERRORthe channel has been closed;
SSH2CHANNEL-TIMEOUTtimeout communicating on channel
SSH2-ERRORsocket error sending data; timeout on socket; invalid SSH2 protocol response; server returned an error message

◆ requestPty()

nothing Qore::SSH2::SSH2Channel::requestPty ( string  term = "vanilla",
string  modes = "",
softint  width = LIBSSH2_TERM_WIDTH,
softint  height = LIBSSH2_TERM_HEIGHT,
softint  width_px = LIBSSH2_TERM_WIDTH_PX,
softint  height_px = LIBSSH2_TERM_HEIGHT_PX,
timeout  timeout = -1 
)

Requests a terminal to be allocated to the channel.

Example:
chan.requestPty("vt100");
Parameters
termterminal emulation requested
modesterminal mode modifier values
widthwidth of the pty in characters
heightheight of the pty in characters
width_pxwidth of the pty in pixels
height_pxheight of the pty in pixels
timeoutan integer giving a timeout in milliseconds or a relative date/time value (ex: 15s for 15 seconds); a negative value means do not time out
Exceptions
SSH2CHANNEL-REQUESTPTY-ERRORnegative terminal or pixel width, height passed
SSH2CHANNEL-ERRORthe channel has been closed
SSH2CHANNEL-TIMEOUTtimeout communicating on channel
SSH2-ERRORsocket error sending data; timeout on socket; invalid SSH2 protocol response; server returned an error message

◆ requestX11Forwarding()

nothing Qore::SSH2::SSH2Channel::requestX11Forwarding ( softint  screen_no = 0,
bool  single_connection = False,
string  auth_proto = "",
string  auth_cookie = "",
timeout  timeout = -1 
)

Request X11 forwarding on the channel.

Example:
chan.requestX11Forwarding(NOTHING, NOTHING, NOTHING, NOTHING, 30s);
Parameters
screen_nothe X11 screen number to forward
single_connectionset to True to forward only a single connection on the channel
auth_protothe authorization protocol to use
auth_cookiethe authorization cookie to use
timeoutan integer giving a timeout in milliseconds or a relative date/time value (ex: 15s for 15 seconds); a negative value means do not time out
Exceptions
SSH2CHANNEL-REQUESTX11FORWARDING-ERRORinvalid stream ID; libssh2 reported an error on the channel while waiting for data from the server
SSH2CHANNEL-ERRORthe channel has been closed
SSH2CHANNEL-TIMEOUTtimeout communicating on channel
SSH2-ERRORsocket error sending data; timeout on socket; invalid SSH2 protocol response; server returned an error message

◆ sendEof()

nothing Qore::SSH2::SSH2Channel::sendEof ( timeout  timeout = -1)

Informs the remote end that we are closing the connection.

Example:
chan.sendEof(30s);
Parameters
timeoutan integer giving a timeout in milliseconds or a relative date/time value (ex: 15s for 15 seconds); a negative value means do not time out
Exceptions
SSH2CHANNEL-SENDEOF-ERRORserver returned an error while waiting for a response
SSH2CHANNEL-ERRORthe channel has been closed
SSH2CHANNEL-TIMEOUTtimeout communicating on channel
SSH2-ERRORsocket error sending data; timeout on socket; invalid SSH2 protocol response; server returned an error message

◆ setEncoding()

nothing Qore::SSH2::SSH2Channel::setEncoding ( string  encoding)

Sets the default string encoding for the channel; all string data read from the channel will be tagged with the encoding given.

Example:
chan.setEncoding("utf8");
Parameters
encodingthe string encoding to set for the channel

◆ setenv()

nothing Qore::SSH2::SSH2Channel::setenv ( string  var,
string  value,
timeout  timeout = -1 
)

Sets an environment variable on the channel.

Example:
chan.setenv("", "", 30s);
Parameters
varthe environment variable to set
valuethe value to set
timeoutan integer giving a timeout in milliseconds or a relative date/time value (ex: 15s for 15 seconds); a negative value means do not time out
Exceptions
SSH2CHANNEL-SETENV-ERRORlibssh2 reported an error on the channel
SSH2CHANNEL-ERRORthe channel has been closed
SSH2CHANNEL-TIMEOUTtimeout communicating on channel
SSH2-ERRORsocket error sending data; timeout on socket; invalid SSH2 protocol response; server returned an error message

◆ shell()

nothing Qore::SSH2::SSH2Channel::shell ( timeout  timeout = -1)

Request a login shell to be started for the channel.

Example:
chan.shell(30s);
Parameters
timeoutan integer giving a timeout in milliseconds or a relative date/time value (ex: 15s for 15 seconds); a negative value means do not time out
Exceptions
SSH2CHANNEL-SHELL-ERRORserver returned an error while waiting for a response
SSH2CHANNEL-ERRORthe channel has been closed
SSH2CHANNEL-TIMEOUTtimeout communicating on channel
SSH2-ERRORsocket error sending data; timeout on socket; invalid SSH2 protocol response; server returned an error message

◆ subsystem()

nothing Qore::SSH2::SSH2Channel::subsystem ( string  command,
timeout  timeout = -1 
)

Executes a command on the channel as a subsystem.

Example:
chan.subsystem("ls -l", 30s);

use SSH2Channel::read() to read the output

Parameters
commandthe command to run as a subsystem
timeoutan integer giving a timeout in milliseconds or a relative date/time value (ex: 15s for 15 seconds); a negative value means do not time out
Exceptions
SSH2CHANNEL-SUBSYSTEM-ERRORlibssh2 reported an error on the channel while waiting for confirmation from the server
SSH2CHANNEL-ERRORthe channel has been closed
SSH2CHANNEL-TIMEOUTtimeout communicating on channel
SSH2-ERRORsocket error sending data; timeout on socket; invalid SSH2 protocol response; server returned an error message

◆ waitClosed()

nothing Qore::SSH2::SSH2Channel::waitClosed ( timeout  timeout = -1)

Wait for the remote end to acknowledge the close request.

Example:
chan.waitClosed(30s);
Parameters
timeoutan integer giving a timeout in milliseconds or a relative date/time value (ex: 15s for 15 seconds); a negative value means do not time out
Exceptions
SSH2CHANNEL-WAITCLOSED-ERRORserver returned an error while waiting for a response
SSH2CHANNEL-ERRORthe channel has been closed
SSH2CHANNEL-TIMEOUTtimeout communicating on channel
SSH2-ERRORsocket error sending data; timeout on socket; invalid SSH2 protocol response; server returned an error message

◆ waitEof()

nothing Qore::SSH2::SSH2Channel::waitEof ( timeout  timeout = -1)

Wait for the remote end to acknowledge an EOF request.

Example:
chan.waitEof(30s);
Parameters
timeoutan integer giving a timeout in milliseconds or a relative date/time value (ex: 15s for 15 seconds); a negative value means do not time out
Exceptions
SSH2CHANNEL-WAITEOF-ERRORserver returned an error while waiting for a response
SSH2CHANNEL-ERRORthe channel has been closed
SSH2CHANNEL-TIMEOUTtimeout communicating on channel
SSH2-ERRORsocket error sending data; timeout on socket; invalid SSH2 protocol response; server returned an error message

◆ write()

nothing Qore::SSH2::SSH2Channel::write ( data  data,
softint  stream_id = 0,
timeout  timeout_ms = -1 
)

Writes data to a stream.

Example:
chan.write(data, 0, 30s);
Parameters
datathe data to write; must be either a string or binary
stream_idthe stream ID to write (0 is the default, 1 is for stderr)
timeoutan integer giving a timeout in milliseconds or a relative date/time value (ex: 15s for 15 seconds); a negative value means do not time out
Exceptions
SSH2CHANNEL-WRITE-ERRORinvalid stream ID; libssh2 reported an error on the channel while waiting for a response from the server
SSH2CHANNEL-ERRORthe channel has been closed
SSH2CHANNEL-TIMEOUTtimeout communicating on channel
SSH2-ERRORsocket error sending data; timeout on socket; invalid SSH2 protocol response; server returned an error message

The documentation for this class was generated from the following file: