BeanShell Scripting

 

Description

 

The sample application in BeanShell makes use of CLI API to create a dedicated CLI Session and send synchronous CLI commands to the device according to the options provided. You are advised to familiarize the BeanShell scripting prior to working with this example.

 

Format

 

java ScriptHandlerExample CliSyncApp localhost -l guest -p guest ls

 

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:

 

import com.adventnet.cli.*;

import com.adventnet.cli.transport.*;

import com.adventnet.cli.messageset.*;

import java.io.*;

 

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();

 

To connect the device, suitable protocol has to be implemented. Use Telnet as a default protocol implementation and set various connection parameters.

 

TelnetProtocolOptionsImpl tpoi=new TelnetProtocolOptionsImpl();

 

The connection parameters such as the remote port name, command prompt, login name, password, etc., can be set. If you are not setting the connection parameters, the default values can be chosen.

 

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

tpoi.setPrompt(values[1]);

tpoi.setLoginPrompt(values[2]);

tpoi.setPasswdPrompt(values[3]);

tpoi.setLoginName(values[4]);

tpoi.setPassword(values[5]);

 

Now, set the CLI message that contains the actual CLI command. Other parameters like the CLIProtocolOptions can be set so that the message is sent to the appropriate device.

 

CLIMessage climsg=new CLIMessage("");

 

Now set the remote host.

 

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

 

Open the CLI Session as given below.

 

clisession=new CLISession(tpoi);

clisession.open();

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

sess.openConnection()

 

Send the CLI message to the device as given below.

 

climsg.setCLIPrompt(tpoi.getPrompt());

System.out.println(clisession.syncSend(climsg).getData());

 

After receiving response from device, close the connection with device.

 

clisession.close();

 

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

 

 

 



Copyright © 2009 ZOHO Corp. All Rights Reserved.