Using regular expressions

Top  Previous  Next

In some situations you may find that the start or end prompts for a SshTask are difficult to define. This is especially true in cases where the prompts may change based on the user or system.

 

To address this issue regular expressions may be optionally used when defining the start and end prompts for a SshTask. To define a prompt as a regular expression first define the prompts and then use the SshTask.IsStartPromptRegex and SshTask.IsEndPromptRegex properties with arguments of true to define the start and end prompts of the SshTask as regular expressions. While a SshTask is running it will use this information to determine whether the data returned by the server should be evaluated using a string comparison match or regular expression match.

 

Example

 

[C#]

 

// 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);

 

// define regular expression

string shellPrompt = "\\$|#|>";

 

// create new task

SshTask task = new SshTask(shellPrompt,"ls -al",shellPrompt);

 

// set start and end prompts as regular expression

task.IsEndPromptRegex = true;

task.IsStartPromptRegex = true;

 

// add task to script

script.AddTask(task);

 

// connect and execute task

ssh.Connect();

 

// wait for task to complete

while(!task.IsComplete) {

 Thread.Sleep(100);

}

 

// print results of task

Console.WriteLine(task.Response);

 

// disconnect

ssh.Disconnect();

 

 

[VB]

 

' create login credentials

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

 

' create new Ssh instance

Dim ssh As Ssh = New Ssh(sshParams)

 

' create new SshScript instance

Dim script As SshScript = New SshScript(ssh)

 

' define regular expression

Dim shellPrompt As String = "\$|#|>"

 

' create new task

Dim task As SshTask = New SshTask(shellPrompt, "ls -al", shellPrompt)

 

' define task start and end prompts as regular expressions

task.IsEndPromptRegex = True

task.IsStartPromptRegex = True

 

' add task to script

script.AddTask(task)

 

' establish connection and run script

ssh.Connect

 

' wait for task to complete

While Not task.IsComplete

Thread.Sleep(100)

End While

 

' print task response to console

Console.WriteLine(task.Response)

 

' disconnect

ssh.Disconnect