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#setStartPromptRegex and SshTask#setEndPromptRegex methods 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

 

// define task  

SshTask lsTask = new SshTask("\\$|#","jsmith","\\$|#");  

lsTask.setStartPromptRegex(true);

lsTask.setEndPromptRegex(true);      

 

In the above example the shell prompt (in this case the start and end prompts) is defined as a regular expression. In the case of the shell prompt the SshTask 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 meta character. This value must be escaped with a "\" and escaped again with a second "\" so that it can be compiled successfully by the Java compiler.

 

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