Asynchronous CLI 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. 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 CliAsyncApp [-s session(d/nd)] [-n RemotePort] [-cp CmdPrompt] [-lp LoginPrompt ] [-pp PasswordPrompt ] [-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 (23).

[-cp]

-

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

[-lp]

-

The Login prompt. This is the prompt that the device issues for getting the user name. Default is 'login:'

[-pp]

-

The Password prompt. This is the prompt that is issued by the device for getting the password. Default is 'Password:'

[-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: M - Mandatory

 

Initially, import the following packages:

 

com.adventnet.cli.*:

com.adventnet.cli.transport.*:

com.adventnet.cli.messageset.*;

java.io.*;

 

The main function contains the following declarations

 

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

[ -cp CmdPrompt] [-lp LoginPrompt ] [-pp PasswordPrompt ]

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

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

String values[] = { null, null, 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 Telnet connections. This contains the remote host, login name, password, and other parameters.

 

TelnetProtocolOptionsImpl tpoi = new TelnetProtocolOptionsImpl();

 

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

 

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

tpoi.setPrompt(values[2]);

tpoi.setLoginPrompt(values[3]);

tpoi.setPasswdPrompt(values[4]);

tpoi.setLoginName(values[5]);

tpoi.setPassword(values[6]);

tpoi.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[7].equals("Set")){

isDebug = true ;

 

To create the CLI messages, instantiate CLIMessageclass. Tthe 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 Telnet 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(tpoi,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.

 

CLIApp = new CLIAsyncApp();

CLIsession.addCLIClient(CLIApp);

try{

climsg.setCLIPrompt(tpoi.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 directory.

 




Copyright © 2009 ZOHO Corp. All Rights Reserved.