|
Description
This sample application makes use of ScriptHandler API to invoke and execute a script file. The script file can be invoked after plugging a scripting implementation into the RunScriptInterface. By default, Python and BeanShell scripting implementations are provided. It also provides facility to plug in custom scripting implementations.
Format
|
Usage: java ScriptHandlerExample scriptFile |
Option
|
ScriptFile |
Mandatory |
The name of file that contains a script. |
|
|
Note: M - Mandatory |
Initially, import the following packages:
|
import com.adventnet.util.script.ScriptHandler; import com.adventnet.cli.transport.TelnetProtocolOptionsImpl; import java.util.*; |
The main function contains the following declarations.
|
String usage = "java ScriptHandlerExample ScriptFile"; if (args.length < 1) { System.err.println(" USAGE : "+usage); |
The script file type and extension is required to use a proper scripting language as each scripting language has different file extension.
|
String scriptFile = args[0]; String scriptType = null; StringTokenizer strTok = new StringTokenizer(scriptFile, "."); if (strTok.countTokens()==2){ strTok.nextToken(); scriptType = strTok.nextToken(); }else{ System.err.println(" File name should have an extension."); System.exit(1); } |
The script file is used to communicate with devices . Telnet is used as a default protocol option. The Telnet options include various connection parameters that can be set in script file.
|
String[] telnetOptions = new String[args.length-1]; |
After setting the Telnet options, the ScriptHandler can be used to execute a suitable scripting type.
|
ScriptHandler sh = new ScriptHandler(); sh.executeScriptFromFile(scriptFile,telnetOptions,scriptType); |
The complete source code is available in <CLI Home>/examples/script directory.
|