Executing tasks dynamically
Top  Previous  Next

Under certain conditions you may want to create and execute tasks on the fly. An example of such a situation might be when the command for the next task depends on the response of the previous task. Dynamic TelnetTask usually have a null start prompt value as they are not waiting on data from the telnet server. To illustrate we will use the following use case.

Example


The example below demonstrates the following using case:

1. Login to system
2. Execute run.sh command
3. If response of run.sh command contains the text "Success" then logout Else execute fail.sh command and logout.


String loginPrompt = "login:";
String passwordPrompt = "Password:"
;
String shellPrompt = "
#";
String username = "jsmith"
;
String password = "secret"
;

// 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, "run.sh"
, shellPrompt);
while (!commandTask.isComplete()) {
  try {
    Thread.sleep(1000
);
  } catch (Exception e) {
  }
}

// commandTask complete … check response

if (commandTask.getResponse().indexOf("Success"
) != -1) {
  // success … logout 

  telnet.disconnect();
else {
  // failure … run fail.sh (no start prompt needed) then logout

  TelnetTask failTask = new TelnetTask(null, "fail.sh"
"#");

  // wait for fail task to complete


  while (!fail.isComplete()) {
    try {
      Thread.sleep(1000
);
    } catch (Exception e) {
    }
  }

  // logout

  telnet.disconnect();
}