SocketTools .NET Edition

Secure Shell Protocol

The Secure Shell (SSH) protocol enables an application to establish a secure, interactive terminal session with a server, or execute commands remotely on the server, with the output of the command returned to the client. The SocketTools .NET class supports both version 1.0 and 2.0 of the protocol.

Initialization

Initialize
Initialize an instance of the class, loading the networking library and validating the development license. This method must be called before any properties are changed or any other methods in this class are called by the application.

Connect
Establish a connection to the server. Once the connection has been established, the other methods in the class may be used to exchange data with the server.

Disconnect
Disconnect from the server and release the memory allocated for that client session. After this method is called, the client session is no longer valid.

Reset
Reset the internal state of the component. This can be useful if your application wishes to discard any settings made by a user and return that instance of the class to its default state.

Uninitialize
Unload the networking library and release any resources that have been allocated for the class instance. This method is automatically invoked when the class instance is disposed or goes out of scope.

Input and Output

Once connected to the server, any output generated by the command shell or a program executed on the server will be sent as data for the client to read. Any input to the program is sent by the client and received and processed by the server. The following methods are used:

Read
Reads any output that has been generated by the program executing on the server. If the server closes the connection, this method will return zero after all the data has been read. If the method is successful, it will return the actual number of bytes read.

ReadLine
The ReadLine method reads data from the server up to the specified number of bytes or until an end-of-line character sequence is encountered. Unlike the Read method which reads arbitrary bytes of data, this function is specifically designed to return a single line of text data in a string variable.

Peek
The Peek method can be used to examine the data that is available to be read from the internal receive buffer. If there is no data in the receive buffer at that time, a value of zero is returned. The Peek method will never cause the client to block, and so may be safely used with asynchronous connections.

Write
Send data to the server which will be received as input to the program. If the method succeeds, the return value is the number of bytes actually written. This method should always be used when sending binary data to the server.

WriteLine
The WriteLine method sends a line of text to the server and terminates the line with a carriage-return and linefeed control character sequence. Unlike the Write method which writes arbitrary bytes of data to the socket, this method is specifically designed to write a single line of text data from a string.

Command Processing

The SSH protocol can be used to connect to a server, log in and execute one or more commands, process the output from those commands and display it to an end-user using a graphical interface. The user never sees or interacts with the actual terminal session. The class interface provides methods which can simplify this kind of application, reducing the amount of code needed to process the data stream returned by the server.

Execute
This method executes a command on a server and copies the output to a user-specified buffer, with the exit code for the remote program as the method's return value. This is a convenience method that enables you to execute a remote command in a single call, without having to write the code to establish the connection and read the output.

Search
This method is used to search for a specific character or sequence of characters in the data stream returned by the server. The control will accumulate all of the data received up to the point where the character sequence is encountered. This can be used to capture all of the output from a command, or search for specific results returned by the command as it executes on the server.