Asynchronous SSH Communication

 

Description

 

This sample example application can be used to send CLI commands to the devices in an asynchronous mode of communication with CLI API using SSH protocol. You can create either dedicated or multiplexed (non-dedicated) CLI sessions.

 

In asynchronous mode, the message is sent to the device, and the session does not wait for the response and returns back.

 

Format

 

java java SshAsyncApp [-s session(d/nd)] [-n RemotePort] [-cp CmdPrompt ] [-l LoginName ] [-p Password] [-d Debug] RemoteHost Command"

 

Options

 

[-s]

-

Session, dedicated (d) or non-dedicated (nd). Default is dedicated.

[-n]

-

Remote port. The default is the telnet port (22).

[-cp]

-

Command prompt. This is the prompt displayed by the device for each command. Default is $.

[-d]

-

To set debug as true or false.

[-l]

-

Login Name. One of the user names/Login names present in the remote host.

[-p]

-

Password for the user.

Remote host

M

The host to which the session has to be established.

Command

M

The actual command that has to be sent to the device.

 

Note

 

1. M - Mandatory

2. To run the example using SSH2 protocol, follow the procedure given in Installation and Setup section.

Initially, import the following packages:

com.adventnet.cli.*;

com.adventnet.cli.transport.*;

com.adventnet.cli.transport.ssh.SshProtocolOptionsImpl;

com.adventnet.cli.transport.ssh.SshTransportProviderImp;

java.io.*;

 

 

The main function contains the following declarations

String usage = "java SshAsyncApp [-s session(d/nd)] [ -n RemotePort]

[ -cp CmdPrompt] [-l LoginName ] [ -p Password ] [-d Debug ] RemoteHost Command";

String options[] = {"-s", "-n", "-cp", "-l", "-p", "-d"};

String values[] = { null, null, null, null, null, "None" };

String cmdData = null;

boolean enablePooling = false;

boolean isDebug = false;

 

ParseOptions class is used to parse the arguments passed to the application to check the proper syntax and validity. In case of wrong syntax,the system throws exception.

 

ParseOptions opt = new ParseOptions(args,options,values,usage);

if(opt.remArgs.length<2){

        opt.usage_error();

Construct the ProtocolOptions for SSH connections. This contains the remote host, login name, password, and other parameters.

SshProtocolOptionsImpl spoi = new SshProtocolOptionsImpl();

 

The following parameters are set to the SSH protocol implementation class so that the transport provider can connect to the device for communication.

 

spoi.setRemotePort(Integer.parseInt(values[1]));

spoi.setPrompt(values[2]);

spoi.setLoginName(values[3]);

spoi.setPassword(values[4]);

spoi.setRemoteHost(opt.remArgs[0]);

 

By setting isDebug as true, the debug can be enabled, and the debug messages can be received in <CLI Home>/log directory.

 

if(values[5].equals("Set")){

    isDebug = true ;

 

To create the CLI messages, instantiate CLIMessage class. The CLIProtocolOptions can be set so that the message is sent to the appropriate device. You have to instantiate this class with the message to be sent and pass it to send () in CLISession class.

 

CLIMessage climsg = new CLIMessage(" ");

CLISession CLIsession = null;

 

Now, create and open CLI session with SSH as a protocol implementation and facility to create a dedicated or non-dedicated session. You can also set the debug level as given below.

 

clisession=new CLISession(spoi,enablePooling);

if( isDebug ) {

    clisession.setDebug(true);

    clisession.setDebugLevel(2);

} else {

    clisession.setDebug(false);

}

clisession.open();

System.out.println("Initial log: " + clisession.getInitialMessage());

 

To receive the response, implement the CLIClient interface, register with CLISession using addCLIClient(), and send the asynchronous CLI message to the device.

 

CLIAsyncApp CLIApp = new CLIAsyncApp();

CLIsession.addCLIClient(CLIApp);

try{

    climsg.setCLIPrompt(spoi.getPrompt());

    clisession.send(climsg);

    }

 

Set the transport provider implementation before opening the session.

 

session.setTransportProviderClassName(String ClassName)

 

The callback method is provided to receive responses from the CLI device while the session sends messages in asynchronous mode. A flag must be set as "true" to wait for the response and to print it inside callback.

 

public boolean callback(CLISession session,CLIMessage msg, int msgId){

        if(msg!=null){

          System.out.println(msg.getData());

          System.exit(1);

         }else {

           System.out.println(" No Response received ");

           System.exit(1);

         }

         return true;

}            

 

The complete source code  is available in <CLI Home>/examples/cli directory.