Executing a script |
A TelnetScript will begin execution as soon as a connection is established with the telnet server. Therefore it is recommended that your TelnetScript be created and all tasks added before the Telnet#connect method is invoked.
The code example below will demonstrate how to automatically login to a telnet server, execute a command and logout. Some assumptions have been made about the start and end prompts. Your system prompts will likely differ although the process would be the same.
Example
String loginPrompt = "login:"; String passwordPrompt = "Password:"; String shellPrompt = "#"; String username = "jsmith"; String password = "secret"; String command = "ls –al";
// build task to submit username TelnetTask loginTask = new TelnetTask(loginPrompt,login,passwordPrompt);
// build task to submit password TelnetTask passwordTask = new TelnetTask(passwordPrompt,password,shellPrompt);
// build task to execute command TelnetTask commandTask = new TelnetTask(shellPrompt,command,shellPrompt);
// add all tasks to script script.addTask(loginTask); script.addTask(passwordTask); script.addTask(commandTask);
// connect to telnet server … script is executed automatically telnet.connect();
// wait until last task is complete while(!commandTask.isComplete()) { try { Thread.sleep(1000); } catch(Exception e) {}
}
// last task completed … disconnect from server telnet.disconnect(); |