AdventNet CLI 2.0 API Specification

com.adventnet.cli.transport.ssh.sshv2
Class SshToolsProviderImpl

com.adventnet.cli.transport.ssh.sshv2.SshToolsProviderImpl

public class SshToolsProviderImpl
implements SshTransportProviderInterface

This class can be used to establish secure telnet connection with a device using SSH2 protocol. It makes use of SshTools for communicating with the remote host using SSH2 protocol. It implements the CLITransportProvider which has methods such as open, read, write.


Fields inherited from interface com.adventnet.cli.transport.CLITransportProvider
STREAM_CLOSED
 
Constructor Summary
SshToolsProviderImpl()
          Constructor for the SshToolsProviderImpl object.
 
Method Summary
 void close()
          This closes the SSH connection to the remote device.
 void connect(com.sshtools.j2ssh.configuration.SshConnectionProperties properties, com.sshtools.j2ssh.transport.HostKeyVerification hostVerification)
          Connects to client to a SSH server
 void connect(java.lang.String hostname)
          Opens connection to a SSH server on the standard port 22 using the default SshHostKeyVerification instance to verify the host key.
 void connect(java.lang.String hostname, int port)
          Connects the client to a SSH server on any given port using the default SshHostKeyVerification instance to verify the host key.
 void connect(java.lang.String hostname, int port, com.sshtools.j2ssh.transport.HostKeyVerification hosts)
          Connects the client to a SSH server using the HostKeyVerification as given by the user.

All connection properties such as cipher and message authentication algorithms are defaulted.
 void disconnect()
          Disconnects the SSH connection
 int getRemoteEOL()
          When the transport protocol negotiates the protocol version, it uses the EOL provided with the protocol identification string as the guessed EOL setting for the remote computer.

The value returned will be any of the following values:

TransportProtocol.EOL_CRLF
TransportProtocol.EOL_LF
 java.lang.String getTerminalType()
          Gets the current terminal type.
 boolean isConnected()
          Determine if the client is connected.
 void login(java.lang.String username, java.lang.String password)
          Logs into the remote host.
 java.lang.String login(java.lang.String username, java.lang.String password, java.lang.String prompt)
          This will log into the device with a particular login name and password.
 void onChannelData(com.sshtools.j2ssh.connection.SshMsgChannelData msg)
          The response is asynchronously obtained here, which is intimated to the session.
 void onChannelExtData(com.sshtools.j2ssh.connection.SshMsgChannelExtendedData msg)
          The response for extended data is obtained here.
 void open(CLIProtocolOptions cliProtOpt)
          Opens a SSH connection to the remote device.
 CLIMessage read()
          Receives the CLI message from the opened telnet connection.
 int read(byte[] b)
          Read decrypted data from the backend.
 void setMessageTimeout(int timeout)
          Sets the timeout for the login or any command sent.
 void setSocketTimeout(int timeout)
          Sets the socket timeout.
 void setTerminalType(java.lang.String term)
          Sets the terminal type of the remote machine.
 java.lang.String waitfor(java.lang.String match)
          Waits for a string to be returned by the remote host and returns all those characters which are received until the match is obtained (including the string being waited for).
 void write(byte[] b)
          Sends the data to the device after encrypting it.
 void write(CLIMessage cliMsg)
          Sends the command in the CLIMessage.
 

Constructor Detail

SshToolsProviderImpl

public SshToolsProviderImpl()
                     throws SshException
Constructor for the SshToolsProviderImpl object.
Method Detail

isConnected

public boolean isConnected()
Determine if the client is connected.
Returns:
true if the connection is still active otherwise false

getRemoteEOL

public int getRemoteEOL()
When the transport protocol negotiates the protocol version, it uses the EOL provided with the protocol identification string as the guessed EOL setting for the remote computer.

The value returned will be any of the following values:

TransportProtocol.EOL_CRLF
TransportProtocol.EOL_LF
Returns:
the guessed EOL value

connect

public void connect(java.lang.String hostname)
             throws ConnectException
Opens connection to a SSH server on the standard port 22 using the default SshHostKeyVerification instance to verify the host key.

When the connection is established with the SSH server for the first time, the host key will be unknown to the Ssh client. Hence, it prompts for the user's input with the following message - "Do you want to allow this host key?[yes/no/always]. Choosing the "always" option, makes an entry for the remote host in the conf/hosts.xml under the tag. The client thereafter recognizes the SSH server. If "yes" is chosen the connection is successfully established during that run and if "no" is chosen, it quits without establishing connection.


All connection properties such as cipher and message authentication algorithms are defaulted.
Parameters:
hostname - the hostname to connect
Throws:
SshException - if a critical error occurs

connect

public void connect(java.lang.String hostname,
                    int port)
             throws ConnectException
Connects the client to a SSH server on any given port using the default SshHostKeyVerification instance to verify the host key.

When the connection is established with the SSH server for the first time, the host key will be unknown to the Ssh client. Hence, it prompts for the user's input with the following message - "Do you want to allow this host key?[yes/no/always]. Choosing the "always" option, makes an entry for the remote host in the conf/hosts.xml under the tag. The client thereafter recognizes the SSH server. If "yes" is chosen the connection is successfully established during that run and if "no" is chosen, it quits without establishing connection.


All connection properties such as cipher and message authentication algorithms are defaulted.
Specified by:
connect in interface SshTransportProviderInterface
Parameters:
hostname - the hostname to connect
port - the port to connect
Throws:
SshException - if a critical error occurs

connect

public void connect(java.lang.String hostname,
                    int port,
                    com.sshtools.j2ssh.transport.HostKeyVerification hosts)
             throws ConnectException
Connects the client to a SSH server using the HostKeyVerification as given by the user.

All connection properties such as cipher and message authentication algorithms are defaulted.
Parameters:
hostname - the hostname to connect
port - the port to connect
hosts - a host key verification implementation for user interaction to verifiy the servers host key.
Throws:
SshException - if a critical error occurs

connect

public void connect(com.sshtools.j2ssh.configuration.SshConnectionProperties properties,
                    com.sshtools.j2ssh.transport.HostKeyVerification hostVerification)
             throws ConnectException
Connects to client to a SSH server
Parameters:
properties - the connection properties to use
hostVerification - a host key verification implementation for user interaction to verifiy the servers host key.
Throws:
SshException - if a critical error occurs

disconnect

public void disconnect()
Disconnects the SSH connection

setTerminalType

public void setTerminalType(java.lang.String term)
Sets the terminal type of the remote machine. This is sent to the remote server during the SSH protocol negotiation.
Parameters:
terminalType - the terminal that the remote machine emulates such as vt100, vt320 or others as a String value.

getTerminalType

public java.lang.String getTerminalType()
Gets the current terminal type.
Returns:
the terminal type as a String value.

setMessageTimeout

public void setMessageTimeout(int timeout)
Sets the timeout for the login or any command sent. when it is set as zero, the command does not timeout and it waits indefinitely.

setSocketTimeout

public void setSocketTimeout(int timeout)
                      throws java.io.IOException
Description copied from interface: SshTransportProviderInterface
Sets the socket timeout. When this is set the commands or login to the device times out.
Specified by:
setSocketTimeout in interface SshTransportProviderInterface
Tags copied from interface: SshTransportProviderInterface
Parameters:
timeout - an int value
Throws:
java.io.IOException - if an error occurs

login

public void login(java.lang.String username,
                  java.lang.String password)
           throws LoginException
Logs into the remote host. The login is performed based on the login and password parameters (uses password authentication). This method assumes that the device prompts for the "login:" and "Password:" prompt respectively.
Parameters:
user - the login name as a string value.
pwd - the password as a string value.

login

public java.lang.String login(java.lang.String username,
                              java.lang.String password,
                              java.lang.String prompt)
                       throws LoginException
Description copied from interface: SshTransportProviderInterface
This will log into the device with a particular login name and password. The login is performed based on the login and password parameter.
Specified by:
login in interface SshTransportProviderInterface
Tags copied from interface: SshTransportProviderInterface
Parameters:
username - the login name
password - the password
prompt - the shell prompt
Returns:
a String value
Throws:
LoginException - if an error occurs

onChannelData

public void onChannelData(com.sshtools.j2ssh.connection.SshMsgChannelData msg)
                   throws com.sshtools.j2ssh.transport.TransportProtocolException,
                          com.sshtools.j2ssh.transport.ServiceOperationException
The response is asynchronously obtained here, which is intimated to the session. Overrides the super class method. This is used internally.
Parameters:
msg - a SshMsgChannelData value
Throws:
TransportProtocolException - if an error occurs
ServiceOperationException - if an error occurs

onChannelExtData

public void onChannelExtData(com.sshtools.j2ssh.connection.SshMsgChannelExtendedData msg)
                      throws com.sshtools.j2ssh.transport.TransportProtocolException,
                             com.sshtools.j2ssh.transport.ServiceOperationException
The response for extended data is obtained here. Overrides the super class method, used internally only. The decrypted data is received here.
Parameters:
msg - a SshMsgChannelExtendedData value
Throws:
TransportProtocolException - if an error occurs
ServiceOperationException - if an error occurs

read

public int read(byte[] b)
         throws java.io.IOException
Read decrypted data from the backend. This is a buffering read. So it will not read from the backend as long as there is data in the buffer.
Specified by:
read in interface SshTransportProviderInterface
Parameters:
b - the buffer to read the decrypted data in
Returns:
the amount of bytes actually read.

write

public void write(byte[] b)
           throws java.io.IOException
Sends the data to the device after encrypting it.
Specified by:
write in interface SshTransportProviderInterface
Parameters:
data - the data/command to be sent as a String value.
Throws:
java.io.IOException - if an error occurs while sending data.

waitfor

public java.lang.String waitfor(java.lang.String match)
                         throws java.io.IOException
Waits for a string to be returned by the remote host and returns all those characters which are received until the match is obtained (including the string being waited for).
Parameters:
match - the string to be received from the remote host which determines the end of response.
Returns:
all data read till the match string is obtained
Throws:
java.io.IOException - when the match string is not received leading to a timeout.

open

public void open(CLIProtocolOptions cliProtOpt)
          throws java.lang.Exception,
                 ConnectException,
                 LoginException
Opens a SSH connection to the remote device. Parameters like the Remote Host, Port, Login name and password are taken from the CLIProtocolOptions parameter that is passed to the method.
Parameters:
cliProtOpt - the CLIProtocolOptions with the parameters.
Throws:
java.lang.Exception - if an error occurs while trying telnet to the remote device.
ConnectException - if unable to connect to the specified host and port.
LoginException - if any of the login parameters like username and password is incorrect.

close

public void close()
This closes the SSH connection to the remote device.
Throws:
java.io.IOException - if an error occurs during the closing of the connection.

write

public void write(CLIMessage cliMsg)
           throws java.io.IOException
Sends the command in the CLIMessage. The CLI command to be sent will be present in a byte array in the CLIMessage argument.
Parameters:
cliMsg - the CLIMessage that has to be sent. The CLI command (as a string) is obtained by calling cliMsg.getData().
Throws:
java.io.IOException - if an error occurs when data is written.

read

public CLIMessage read()
                throws java.io.IOException
Receives the CLI message from the opened telnet connection. The CLI response is read into the CLIMessage and returned.
Returns:
CLIMessage that is read.
Throws:
java.io.IOException - if an error occurs while reading the data through the telnet connection.

AdventNet CLI 2.0 API Specification