Using regular expressions |
In some situations you may find that the login, password or shell prompts for a TelnetSession 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 login, password or shell prompts for a TelnetSession. To define a prompt as a regular expression you may use the LoginPrompt, PasswordPrompt or ShellPrompt properties to define the respective prompts of the TelnetSession as regular expressions and then use the ShellPromptRegex, LoginPromptRegex, and PasswordPromptRegex properties to define the prompts of the TelnetSession as regular expressions. While a TelnetSession 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 session and define prompts as regular expressions
TelnetSession session = new TelnetSession(hostname);
session.ShellPrompt = "\\$|#|>";
session.ShellPromptRegex = true;
session.LoginPrompt = "[Ll]ogin:";
session.LoginPromptRegex = true;
session.PasswordPrompt = "[Pp]assword";
session.PasswordPromptRegex = true;
[Visual Basic]
Private session As TelnetSession
' create session and define prompts as regular expressions
session = New TelnetSession(hostname)
session.ShellPrompt = "\\$|#|>"
session.ShellPromptRegex = True
session.LoginPrompt = "[Ll]ogin:"
session.LoginPromptRegex = True
session.PasswordPrompt = "[Pp]assword"
session.PasswordPromptRegex = True
In the above example the shell, login and password prompts are defined as regular expressions. In the case of the shell prompt the TelnetSession will wait for a "$", "#", or ">" to know when the shell prompt has been displayed. Notice that the "$" in the 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 "\" so that it can be compiled successfully.
In the case of the login prompt the TelnetSession will wait for the text "Login:" or "login:" to be returned by the server before submitting the username. And finally in the case of the password prompt the TelnetSession will wait for the text "Password:" or "password:" to be returned by the server before sending the password.
See Also
For a tutorial on using regular expressions the following website is recommended:
http://www.regular-expressions.info/