The SshClient class provides the ability to connect to a remote system using SSH2
or SSH3, but not SSH1. This class is very similar to the TelnetClient class.
Constructor
var client = new SshClient( String ip, String userName, String password, int port );
Specifying the
port is optional. If the
port is not specified, the default SSH port of 22 is used.
Properties
There are no SshClient properties accessible from JavaScript.
Methods
Return Value | Name | Description |
String[] | expect( String regex ) | Matches regex on incoming text, returns all capture groups. |
String | get( String regex ) | Blocks until regex is matched in returning text; returns everything up to that point or times out. |
String | getAll() | Returns all incoming text since the last 'get' oriented method was called. Imposes a 200ms wait for each invocation. |
String | getAllToPrompt( String prompt ) | Returns all input in the buffer up to prompt, since the last send. |
int | getFlags() | Returns the flags used to compile the regex . Refer to the Java class Pattern for more information. |
String | getPrompt() | Sends a newline and returns the results. Try to minimize the use of this method because it imposes a one second wait every time it is called. |
String[] | grep( String text , String regex ) | Performs basic grep functionality on text using regex . |
String | replaceAll( String regex , String replace , String text ) | Replaces all occurrences of regex in text with replace |
String | replaceFirst( String regex , String replace , String text ) | Replaces the first occurrence of regex in text with replace |
void | send( String value ) | Writes a string value to the SSH session |
void | sendAsIs( String value ) | Writes a string value to the SSH session without a trailing newline. |
void | setCaseInsensitive( boolean value ) | Enables/disables case insensitive pattern matching. |
void | setCommandPrompt( String prompt ) | Sets the command prompt to expect. Default is "\\$ ?" |
void | setDotall( boolean value ) | Enables/disables dotall mode. In dotall mode, the expression . matches any character, including a line terminator. By default this expression does not match line terminators. This allows matching patterns across multiple lines. |
void | setFlags( int flags ) | Sets the flags used to compile the regex. |
void | setMultiline( boolean value ) | Enables multiline mode, in which the expressions ^ and $ match just after or just before, respectively, a line terminator or the end of the input sequence. By default these expressions only match at the beginning and the end of the entire input sequence. |
void | setTimeout( int seconds ) | Sets the timeout value of all 'get' type operations (get, expect, etc). Default is 10 seconds. |
void | wait( int milliseconds ) | Blocks for specified number of milliseconds |
The following table lists the flag values and their meanings.
Value | Description |
1 | Enables Unix lines mode |
2 | Enables case-insensitive matching |
4 | Permits whitespace and comments in pattern |
8 | Enables multiline mode |
16 | Enables literal parsing of the pattern |
32 | Enables dotall mode |
64 | Enables Unicode-aware case folding |
128 | Enables canonical equivalence |
Sample usage
var client = new SshClient( “1.2.3.4”, “root”, “password” ); client.setCommandPrompt( “Select menu option: “ ); client.getPrompt(); client.send( “logout” );