Using regular expressions

Top  Previous  Next

In some situations you may find that the login, password or shell prompts for a SshSession 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 shell prompt for a SshSession. To define the shell prompt as a regular expression you may use the SshSession.SetShellPrompt method passing in the regular expression and true as arguments. While a SshSession 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#]

 

// define regular expression

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

 

// create new SshSession instance

SshSession session = new SshSession(hostname,username,password);

 

// set shell prompt as regular expression

session.SetShellPrompt(shellPrompt,true);

 

[VB]

 

' define regular expression

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

 

' create new SshSession instance

Dim session As SshSession = New SshSession(hostname, username, password)

 

' set shell prompt as regular expression

session.SetShellPrompt(shellPrompt, True)

 

In the above example the shell prompt is defined as a regular expression. In the case of the shell prompt the SshSession will wait for a $ or # to know when the shell prompt has been displayed. Notice that the $ in the C# regular expression is preceded by two slashes. This is required because in regular expressions the $ character is known as a metacharacter. This value must be escaped with a \ and escaped again with a second \ in C# so that it can be compiled successfully.

 

For a tutorial on using regular expressions the following website is recommended:

 

http://www.regular-expressions.info/