Back to product page

Execute method


Executes blocking method and waits until it completes.

Type

String

Syntax

  • C#
  • VB.NET
public String Execute(String Command);
The Execute(Command) syntax has these parts:
CommandCommand that will be executed.
Return valueData received while Execute was running.

public String Execute(String Command, String Prompt);
The Execute(Command,Prompt) syntax has these parts:
CommandCommand that will be executed.
PromptString that should be received to notify as command success. (exact text or regular expression).
Return valueData received while Execute was running.

public String Execute(String Command, String Prompt, Int16 Timeout);
The Execute(Command,Prompt,Timeout) syntax has these parts:
CommandCommand that will be executed.
PromptString that should be received to notify as command success. (exact text or regular expression).
TimeoutSeconds to wait for successful completion of the command.
Return valueData received while Execute was running.

public Function Execute(ByVal Command As String) As String
The Execute(Command) syntax has these parts:
CommandCommand that will be executed.
Return valueData received while Execute was running.

public Function Execute(ByVal Command As String, ByVal Prompt As String) As String
The Execute(Command,Prompt) syntax has these parts:
CommandCommand that will be executed.
PromptString that should be received to notify as command success. (exact text or regular expression).
Return valueData received while Execute was running.

public Function Execute(ByVal Command As String, ByVal Prompt As String, ByVal Timeout As Int16) As String
The Execute(Command,Prompt,Timeout) syntax has these parts:
CommandCommand that will be executed.
PromptString that should be received to notify as command success. (exact text or regular expression).
TimeoutSeconds to wait for successful completion of the command.
Return valueData received while Execute was running.

Remarks

Execute method will send command of your choice to the server, and wait until it is completed. If it does not return in specified time, timeout error is generated.

There's no general way of knowing what to expect from command to return once you execute it, so you must use Prompt argument to specify what to expect to be received after last line of the command. Most usually, this will be your command prompt, but depending on the command you execute, it can be something else - meaningful to you.

NOTE: this method works only when Blocking property is set to True.

Very common way for using Execute method is to send few commands sequentially, waiting for each to return something and probably make a decision on what to do next. Without usage of Execute method, you would need some complicated algorithms - so Execute comes here to help. Don't forget to put CRLF ("\r\n") sequence at the end of your command, since wodSSH will not do that for you (we never know if you actually want it).

Typically, you might want to use it like this:
 
Ssh1.Blocking = true;
Ssh1.Connect();
 
Console.Write(Ssh1.Waitfor("joe@debian"));
Ssh1.Prompt = "joe@debian";
Console.Write(Ssh1.Execute("cd /tmp\r\n"));
Console.Write(Ssh1.Execute("ls -al\r\n"));
 

and it will do the following - connect to the server, wait everything until prompt is received (I assume here that joe@debian is prompt that is expected to be received), and then change directory to /tmp and list all files.
Good thing is that if Execute method fails, you will still be able to call Receive method to read data in buffer - if any.
Note - this command internally disables PromptReceived event and DataReceived event since it's taking control on what's received from the server.

As mentioned above, you can provide regular expression to be set as expected response. You should prepend Prompt argument with regex: text so wodSSH.NET knows you are using regular expression. For example, you can use it like this:
 
Console.Write(Ssh1.Execute("cd /tmp\n", "regex:[\\$%#>]$"));
 

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 will search for exact match of specified pattern.

Platforms

Windows