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.

 

Use 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.

 

Example

 

[C#]

using System;

using System.Threading;

using Jscape.Telnet;

 

namespace TelnetScriptExample {

 

       public class TelnetScriptExample {

 

               string user = "username"; // Specify user name to login

               string pass = "password"; // Specify password to login

               string host = "telnet.server.com"; // Specify telnet server

               string prompt = "$";

               public Telnet MyTelnet = null;

 

               public TelnetScriptExample(){

 

                       MyTelnet = new Telnet(host);

 

                       // Subscribe to connection events

                       MyTelnet.ConnectedEvent += new Telnet.ConnectedEventHandler(OnConnected);

                       MyTelnet.DisconnectedEvent += new Telnet.DisconnectedEventHandler(OnDisconnected);

 

                       // Subscribe to option events

                       MyTelnet.DoOptionEvent += new Telnet.DoOptionEventHandler(OnDoOption);

                       MyTelnet.DontOptionEvent += new Telnet.DontOptionEventHandler(OnDontOption);

                       MyTelnet.WillOptionEvent += new Telnet.WillOptionEventHandler(OnWillOption);

                       MyTelnet.WontOptionEvent += new Telnet.WontOptionEventHandler(OnWontOption);

 

                       // Subscribe to data received event

                       MyTelnet.DataReceivedEvent += new Telnet.DataReceivedEventHandler(OnDataReceived);

 

                       // Create a TelnetScript

                       TelnetScript script = new TelnetScript(MyTelnet);

 

                       // Create script tasks

                       TelnetTask login = new TelnetTask("login:", user, "Password:");

                       TelnetTask password = new TelnetTask("Password:", pass, prompt);

                       TelnetTask sh_exec = new TelnetTask(prompt, "run.sh", prompt);

 

                       // Subscribe to task started event

                       script.TaskStartedEvent += new TelnetScript.TaskStartedEventHandler(OnTaskStarted);

 

                       // Subscribe to task ended event

                       script.TaskEndedEvent += new TelnetScript.TaskEndedEventHandler(OnTaskEnded);

 

                       // Subscribe to task failed event

                       script.TaskFailedEvent += new TelnetScript.TaskFailedEventHandler(OnTaskFailed);

 

                       // Add tasks to script

                       script.AddTask(login);

                       script.AddTask(password);

                       script.AddTask(sh_exec);

 

                       // Connect to telnet server

                       MyTelnet.Connect();

 

                       while(!sh_exec.IsComplete()) {

                               try {

                                       Thread.Sleep(1000);

                               } catch(Exception e) {}

 

 

                               // sh_exec complete - check response

                               if(sh_exec.GetResponse().IndexOf("Success") != -1) {

                                       // success - logout

                                       MyTelnet.Disconnect();

                               } else {

                                       // failure - run fail.sh (no start prompt needed) then logout

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

 

                                       // wait for fail task to complete

                                       while(!failTask.IsComplete()) {

                                               try {

                                                       Thread.Sleep(1000);

                                               } catch(Exception e) {}

                                       }

 

                               }

 

                               // Disconnect from telnet server

                               MyTelnet.Disconnect();

                       }

               }

 

               public static void Main() {

                       // Create new telnet example

                       TelnetScriptExample telnetScriptExample = new TelnetScriptExample();

               }

 

               public virtual void OnTaskStarted(object sender, TelnetTaskStartedEventArgs e) {

               }

               public virtual void OnTaskEnded(object sender, TelnetTaskEndedEventArgs e) {

               }

               public virtual void OnTaskFailed(object sender, TelnetTaskFailedEventArgs e) {

                       // Write the task name and disconnect

                       Console.WriteLine("Task " + e.Task.Command + " failed.");

                       MyTelnet.Disconnect();

               }

 

               public void OnConnected(object sender, TelnetConnectedEventArgs e) {

                       Console.WriteLine("Connected to {0}:{1}", e.Host, e.Port);

               }

               public void OnDisconnected(object sender, TelnetDisconnectedEventArgs e) {

                       Console.WriteLine("Disconnected.");

               }

               public void OnDoOption(object sender, TelnetDoOptionEventArgs e) {

                       MyTelnet.SendWontOption(e.Option);

               }

               public void OnDontOption(object sender, TelnetDontOptionEventArgs e) {

                       MyTelnet.SendDontOption(e.Option);

               }

               public void OnWillOption(object sender, TelnetWillOptionEventArgs e) {

                       MyTelnet.SendDontOption(e.Option);

               }

               public void OnWontOption(object sender, TelnetWontOptionEventArgs e) {

                       MyTelnet.SendWontOption(e.Option);

               }

               public void OnDataReceived(object sender, TelnetDataReceivedEventArgs e) {

                       Console.Write(MyTelnet.Encoding.GetString(e.Data));

               }

       }

}

 

 

[Visual Basic]

Imports System

Imports System.Threading

Imports Jscape.Telnet

 

Public Class TelnetExample

 

   Private WithEvents myTelnet As Telnet

   Private WithEvents myScript As TelnetScript

 

   Public Sub New()

 

       myTelnet = New Telnet("10.0.0.1")

       ' Create a TelnetScript

       myScript = New TelnetScript(myTelnet)

 

       ' Create script tasks

       Dim login As TelnetTask = New TelnetTask("login:", "username", "Password:")

       Dim password As TelnetTask = New TelnetTask("Password:", "password", "$")

       Dim sh_exec As TelnetTask = New TelnetTask("$", "run.sh", "$")

       Dim failTask As TelnetTask = New TelnetTask(vbNull, "fail.sh", "$")

 

       ' Add tasks to script

       myScript.AddTask(login)

       myScript.AddTask(password)

       myScript.AddTask(sh_exec)

 

       ' Connect to telnet server

       myTelnet.Connect()

 

       While Not sh_exec.IsComplete()

           Try

               Thread.Sleep(1000)

           Catch e As Exception

           End Try

       End While

 

       ' sh_exec complete - check response

       If (sh_exec.GetResponse().IndexOf("Success") <> -1) Then

           ' success - logout

           myTelnet.Disconnect()

       Else

           ' failure - run fail.sh (no start prompt needed) then logout

           myScript.AddTask(failTask)

           ' wait for fail task to complete

           While Not failTask.IsComplete

               Try

                   Thread.Sleep(1000)

               Catch e As Exception

               End Try

           End While

       End If

 

       ' Disconnect from telnet server

       myTelnet.Disconnect()

 

   End Sub

 

   Public Shared Sub main()

       ' Create new TelnetExample instance passing telnet server hostname or IP address

       Dim example As TelnetExample

       example = New TelnetExample

 

   End Sub

 

   Private Sub OnConnected(ByVal sender As Object, ByVal e As TelnetConnectedEventArgs) Handles myTelnet.ConnectedEvent

       Console.WriteLine("Connected to {0}:{1}", e.Host, e.Port)

 

   End Sub

 

   Private Sub OnDisconnected(ByVal sender As Object, ByVal e As TelnetDisconnectedEventArgs) Handles myTelnet.DisconnectedEvent

       Console.WriteLine("Disconnected")

 

   End Sub

 

   Private Sub OnDoOption(ByVal sender As Object, ByVal e As TelnetDoOptionEventArgs) Handles myTelnet.DoOptionEvent

       myTelnet.SendWontOption(e.Option)

 

   End Sub

 

   Private Sub OnDontOption(ByVal sender As Object, ByVal e As TelnetDontOptionEventArgs) Handles myTelnet.DontOptionEvent

       myTelnet.SendDontOption(e.Option)

 

   End Sub

 

   Private Sub OnWillOption(ByVal sender As Object, ByVal e As TelnetWillOptionEventArgs) Handles myTelnet.WillOptionEvent

       myTelnet.SendDontOption(e.Option)

 

   End Sub

 

   Private Sub OnWontOption(ByVal sender As Object, ByVal e As TelnetWontOptionEventArgs) Handles myTelnet.WontOptionEvent

       myTelnet.SendWontOption(e.Option)

 

   End Sub

 

   Private Sub OnDataReceived(ByVal sender As Object, ByVal e As TelnetDataReceivedEventArgs) Handles myTelnet.DataReceivedEvent

       Console.WriteLine(myTelnet.Encoding.GetString(e.Data))

 

   End Sub

 

   Private Sub OnTaskStarted(ByVal sender As Object, ByVal e As TelnetTaskStartedEventArgs) Handles myScript.TaskStartedEvent

   End Sub

 

   Private Sub OnTaskEnded(ByVal sender As Object, ByVal e As TelnetTaskEndedEventArgs) Handles myScript.TaskEndedEvent

   End Sub

 

   Private Sub OnTaskFailed(ByVal sender As Object, ByVal e As TelnetTaskFailedEventArgs) Handles myScript.TaskFailedEvent

       ' Write the task name and disconnect

       Console.WriteLine("Task " + e.Task.Command + " failed.")

       myTelnet.Disconnect()

   End Sub

 

End Class