Scripting

 

Scripting can be used for automating complex CLI device configuration tasks. The ScriptHandler is capable of running different types of scripts such as Python, BeanShell, TCL, and others. It can be used to execute single line of script as well as script from a file. The arguments can also be passed to the file, containing the script, during its execution.

 

The default implementations for launching Python and BeanShell scripts are provided, which can be used to execute the Python and BeanShell scripts. The ScriptHandler also provides an extensive support to plug in custom script interpreter implementations.

 

The scripting is the utility module that consists of the following classes.

  1. ScriptHandler

  2. ScriptInterface

  3. Python Scripting Implementation

  4. BeanShell Scripting Implementation

The following diagram explains different stages involved in execution of the Script using CLI.

 

 

ScriptHandler

 

ScriptHandler class is used to invoke the scripts for CLI application. It maintains a list of scripting languages and RunScriptInterface implementations, as defined in the scripthandler.conf file under <CLI Home>/conf directory. The script handler can be used for invoking different scripting language scripts.

 

Method Purpose

setScriptType(java.lang.String stype)

This method can be used to set the script language of the ScriptHandler.

setRunScriptIfcImpl(RunScriptInterface runScript)

This method can be used to set the RunScriptInterface implementation class.

setRunScriptIfcImpl(java.lang.String stype)

This method can be used to set the RunScriptInterface implementation class for the script type.

loadScriptProperties(java.lang.String sType, java.util.Properties prop)

This method can be used to initialize the scriptType with the given properties.

addRunScriptIfcImplClassName(java.lang.String sType, java.lang.String className)

This method can be used to add a scriptType and its corresponding class dynamically to the already existing list.

 

The following code snippet explains how to plug in a script written in BeanShell.

 

ScriptHandler sh = new ScriptHandler();

sh.executeScriptFromFile("test.bsh");

 

For more details, please refer to the Script Handling example application available in Examples chapter.

 

Script Interface

 

RunScriptInterface interface is used to implement script handling of any scripting language. The interface methods are called by the ScriptHandler class to execute the scripts. The methods of the interface should be implemented appropriately to execute the scripts. Hence, any script handling implementation can be plugged into the ScriptHandler.

 

Python Scripting Implementation

 

PythonScriptRunnerImpl class is used to execute the scripts written in Python. The Python Interpreter is used to execute the script. This class contains the following methods.

 

Method Purpose

init(java.util.Properties prop)

This method can be used to initialize a list of properties.

executeScript(java.lang.String scriptFileName, java.lang.String[] args)

This method can be used to execute a Python script with file name and arguments to be passed to the script.

executeLine(java.lang.String scriptLine)

This method can be used to execute a Python script line by line.

 

Python scripts can be launched by filing the PythonScriptRunnerImpl in scripthandler.conf file as given below that is available in <CLI Home>/conf directory.

 

py com.adventnet.util.script.PythonScriptRunnerImpl

 

For more details, please refer to the Python Scripting example available in Examples chapter.

 

BeanShell Scripting Implementation

 

BeanShellScriptRunnerImpl class is used to execute the scripts written in BeanShell. The BeanShell Interpreter is used to execute the script. This class contains the following methods.

 

Method Purpose

init(java.util.Properties prop)

This method can be used to initialize a list of properties.

executeScript(java.lang.String scriptFileName, java.lang.String[] args)

This method can be used to execute a BeanShell script with filename and arguments to be passed to the script.

executeLine(java.lang.String scriptLine)

This method can be used to executes a BeanShell script line by line.

 

BeanShell scripts can be launched by filing the BeanShellScriptRunnerImpl in scripthandler.conf file as given below that is available in <CLI Home>/conf directory.

 

bsh com.adventnet.util.script.BeanShellScriptRunnerImpl

 

For more details, please refer to the BeanShell Scripting example available in Examples chapter.

 

How to Plug in Scripting Implementation

 

ScriptHandler API provides an extensive support to plug in any scripting languages. The following procedure can be followed to plug in a custom scripting language:

Note:

  1. Scripting languages and the RunScriptInterface implementations can be added dynamically to the ScriptHandler, but the addition would not get reflected in the scripthandler.conf file in <CLI Home>/conf directory.

  2. If more than one entry is specified for the same scripting language in the scripthandler.conf file, only the RunScriptInterface implementation class of the last entry for the scripting language will be accepted.

 

Please refer to Java documentation for complete details.

 

Related Topics

 

 



Copyright © 2009 ZOHO Corp. All Rights Reserved.