Using regular expressions

Top  Previous  Next

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 TelnetSession#setLoginPrompt, TelnetSession#setPasswordPrompt or TelnetSession#setShellPrompt methods passing in the regular expression and true as arguments to define the respective 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

 

// define prompts

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

String loginPrompt = "[Ll]ogin:";

String passwordPrompt = "[Pp]assword";

 

// create session and define prompts as regular expressions

TelnetSession session = new TelnetSession(hostname);

session.setShellPrompt(shellPrompt,true);

session.setLoginPrompt(loginPrompt,true);

session.setPasswordPrompt(passwordPrompt,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 by the Java compiler.

 

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.

 

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

 

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





Home | Company | Products | Solutions | Purchase | Support | Services | Blog

© 2021 JSCAPE LLC