Back to product page

Prompt property


Specifies command prompt string that is expected from the server.

Type

String. Represents command prompt string. (exact text or regular expression).

Syntax

  • C#
  • VB.NET
String Prompt {get; set; };

Property Prompt As String

Remarks

Prompt property allows wodSSH.NET to have 'a feature' to determine if last executed command (and it's output) finished, so new commands are to be executed.

If you have more than one command to execute on remote server, you will find this feature handy. If you set it up property, wodSSH.NET will fire PromptReceived event each time it recognizes command prompt sequence.

Take an example, try to connect to your server. After connection is established, you will receive something like this:
 
               telnet some.host.com
               Trying some.host.com...
               Connected to 192.168.1.10.
               Escape character is '^]'.
 
               somehost login: joe
               Password:
 
               Linux 2.4.5.
               Last login: Sat Jan 26 2002 on pts/0 from mainframe.
               No mail.
               joe@somehost:~$            
 

note here that sequence 'joe@somehost:' represents command prompt string that will be returned by server each time it expects new command. To be even more certain, you can add CRLF sequence before it.

So, if you set Prompt = "joe@somehost:" then each time wodSSH.NET finds this sequence it will fire PromptReceived event. Here, you can execute new command, and wait for it's output.
If Prompt property is set, the command prompt string you entered will not be provided through DataReceived event - which means you don't have to 'cut it off' from command output.

Good choice of setting prompt string would be something general like ##PROMPT## or similar. This way you don't depend on username or hostname, or even system type. However, you depend on one thing - being able to set such prompt on server as soon as you connect. So, immediately when you connect you issue command
 
export PS1 = '##PROMPT##'
 

and server will set that prompt for the future (until you disconnect). There is one problem here, however. Since all you type (in fact, send to the server) is returned as echo back to you - so issuing this command may be misinterpreted by wodSSH.NET and PromptReceived event may be fired even prompt isn't received at all. So, it's better to define something like
 
export PS1 = '##PROMPT##\\##'
 

Why this? Notice double backslashes? In fact, using double backslashes means that you want server to use one only - but since it's special character on the server you must enter it twice. Now set up Prompt to "##PROMPT##\##" (one backslash) and you will get a good result - echo will be different (because you sent two backslashes) and command prompt will have only one (so you set property properly). Please note that 'export PS1' command is used with bash shell, and may be different on other servers. Before using it, make sure it will product the result you expect.

As mentioned above, you can provide regular expression to be searched in received data instead of exact value. You should prepend your Prompt property with regex: text so wodSSH.NET knows you are using regular expression. For example, you can use it like this:
 
       Ssh.HostName = "some.host.com";
       Ssh.Login = "joe";
       Ssh.Password = "secretpass";
       Ssh.Protocol = SSHAuto;
       Ssh.Prompt = "regex:[\\$%#>] $";
       Ssh.Connect();
 

above regular expression will match when:
  • $, %, #, or > found
  • space
  • end of line
typically, our previous sample joe@somehost:~$ does match above regex. But, hey, feel free to use something more complex such as:
 
Console.Write(Ssh1.Execute("cd /tmp\n", "regex:[a-zA-z0-9]+@[a-zA-z0-9]+:[a-zA-z0-9~/]+[\\$%#>] $"));
 

or using extended PERL syntax
 
Console.Write(Ssh1.Execute("cd /tmp\n", "regex:\\w+@\\w+:\\S+\\$ $"));
 

just don't forget to put 'regex:' text in front of regular expression, otherwise wodSSH.NET will search for exact match of specified pattern.

Platforms

Windows