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 SshTask usually have a null start prompt value as they are not waiting on data from the SSH server. To illustrate we will use the following use case.

 

Example

 

The example below demonstrates the following use case:

 

1. Login to system

2. Execute "ls -al *.tmp" command to get all files ending in .tmp extension.

3. If response of command contains .tmp (indicating that files with .tmp extension exist) then issue command "rm *.tmp" to delete all temporary files.

 

[C#]

 

using System;

using Jscape.Ssh;

using System.Threading;

 

namespace TestSsh

{

 public class SshTaskExample

 {

         public SshTaskExample()

         {

                 string hostname = "www.myhost.com";

                 string username = "jsmith";

                 string password = "secret";

 

                 // create login credentials

                 SshParameters sshParams = new SshParameters(hostname,username,password);

 

                 // create new Ssh instance

                 Ssh ssh = new Ssh(sshParams);

 

                 // create new SshScript instance

                 SshScript script = new SshScript(ssh);

 

                 // create new task

                 SshTask dirTask = new SshTask("$","ls -al *.tmp","$");

 

                 // add task to script

                 script.AddTask(dirTask);                        

 

                 // connect and execute task

                 ssh.Connect();

 

                 // wait for task to complete

                 while(!dirTask.IsComplete) {

                         Thread.Sleep(100);

                 }

 

                 // get task response

                 string response = dirTask.Response;

 

                 SshTask delTask = null;

 

                 // check response of previous task

                 if(response.IndexOf(".tmp") != -1) {

                         // create new task to delete files

                         delTask = new SshTask(null, "rm *.tmp", "$");

 

                         // add to script and execute

                         script.AddTask(delTask);

                 }

 

                 // wait for script to complete

                 while(!script.IsComplete) {

                         Thread.Sleep(100);

                 }                

 

                 // disconnect

                 ssh.Disconnect();

 

                 Console.WriteLine("Done");

                 

         }

 

         static void Main(string[] args) {

                 SshTaskExample example = new SshTaskExample();

         }

 }

}

 

[VB]

 

Imports System

Imports Jscape.Ssh

Imports System.Threading

Namespace TestSsh

 

Public Class SshTaskExample

 

  Public Sub New()

    Dim hostname As String = "www.myhost.com"

    Dim username As String = "jsmith"

    Dim password As String = "secret"

 

    ' create login credentials

    Dim sshParams As SshParameters = New SshParameters(hostname, username, password)

 

    ' create new Ssh instance

    Dim ssh As Ssh = New Ssh(sshParams)

 

    ' create script

    Dim script As SshScript = New SshScript(ssh)

 

    ' create task to perform directory listing

    Dim dirTask As SshTask = New SshTask("$", "ls -al *.tmp", "$")

 

    ' add task to script

    script.AddTask(dirTask)

 

    ' connect and execute script

    ssh.Connect

 

    ' wait for task to complete

    While Not dirTask.IsComplete

      Thread.Sleep(100)

    End While

 

    ' get task response

    Dim response As String = dirTask.Response

 

    ' create task to delete files if files found

    Dim delTask As SshTask = Nothing

    If Not (response.IndexOf(".tmp") = -1) Then

      delTask = New SshTask(Nothing, "rm *.tmp", "$")

      script.AddTask(delTask)

    End If

 

    ' wait for script to complete

    While Not script.IsComplete

      Thread.Sleep(100)

    End While

 

    ' disconnect

    ssh.Disconnect

    Console.WriteLine("Done")

  End Sub

 

  Shared Sub Main(ByVal args As String())

    Dim example As SshTaskExample = New SshTaskExample

  End Sub

End Class

End Namespace