|
Description
This example explains how to parse the response into table format from a CLI device by issuing commands that display the output in table form. The parser uses an XML-based rule set to parse the response for a particular command.
A sample XML is provided for commands such as df,ls,and route for UNIX machines. The example application connects the device based on the input options provided in the command line. It sends the command to the device and with the help ParserTableExample.xml rule file that is available in <CLI Home>/examples/parser parses the response returned by the device.
Format
|
java ParserTableExample [-n RemotePort] [-cp CmdPrompt] [-lp LoginPrompt] [-pp PasswordPrompt] [-l LoginName ] [ -p Password ] TargetHost command [columnNames] |
Options
|
[-n] |
- |
Target 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:' |
|
[-l] |
|
The Login or User Name for the login. If this is not provided, it's assumed that a user name is not required for login. |
|
[-p] |
|
The Password for the login. If this is not specified, it's assumed that the password is not required. |
|
[columnNames] |
|
The list of column names whose value is to be extracted and displayed. |
|
Target host |
M |
The hostname or IP address to which the connection has to be established. |
|
command |
M |
The command to be executed and whose response needs to be parsed. This should be one of those in the ParserTableExample.xml file in <CLI Home>/examples/parser directory. |
|
|
Note: M - Mandatory |
Initially, import the following packages:
|
import com.adventnet.cli.*; import com.adventnet.cli.transport.*; import com.adventnet.util.parser.*; import com.adventnet.util.parser.line.*; import java.util.*; |
Now, create a TelnetProtocolOptionsImpl for connecting with the device.
|
TelnetProtocolOptionsImpl tpoi=new TelnetProtocolOptionsImpl(); |
Set various protocol options such as remote host, login name, password, etc., to connect the device.
|
TelnetProtocolOptionsImpl tpoi=new TelnetProtocolOptionsImpl(); |
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(); |
You can either set the default values for the protocol options or pass the values while executing the application.
|
tpoi.setRemotePort(Integer.parseInt(values[0])); tpoi.setPrompt(values[1]); tpoi.setLoginPrompt(values[2]); tpoi.setPasswdPrompt(values[3]); tpoi.setPasswdPrompt("Password"); tpoi.setLoginName(values[4]); tpoi.setPassword(values[5]); tpoi.setRemoteHost(opt.remArgs[0]); command = opt.remArgs[1]; |
Now, set the command to be sent to the device.
|
msg.setData(command); |
The XML rule file that is used to parse the response from device into a table form can be set using the following main function.
|
ParserAPI api = new ParserAPI("ParserScalarExample.xml"); |
The CLI response in the table form can be received using the following method.
|
ParsedResult result = (ParsedResult) api.parse(response.getData(),"rule1",commandName); |
Now, pick up the properties object, which contain all the parameters extracted from the response.
|
Hashtable table = result.getTableResult(); |
If you are not specifying the column names at the command line, all the columns defined in the XML file and the respective values are displayed.
|
if(columns==null){ Enumeration en = table.keys(); while(en.hasMoreElements()){ String parameter = (String) en.nextElement(); System.out.println("Column: " + parameter); System.out.println("Values: "); ArrayList paramValues = (ArrayList)table.get(parameter); for(i=0;i<paramValues.size();i++){ System.out.println(paramValues.get(i)); } System.out.println(); } |
You can also display the values of those columns that are specified in the command line.
|
for(i=0;i<columns.length;i++){ ArrayList paramValues = (ArrayList)table.get(columns[i]); if(paramValues!=null){ System.out.println("Column: " + columns[i]); System.out.println("Values: "); for(int k=0;k<paramValues.size();k++){ System.out.println(paramValues.get(k)); } System.out.println(); } |
The CLISession can be closed after receiving the parsed response from the device as given below.
|
sess.close(); |
The complete source code is available in <CLI Home>/example/parser directory.
|