Back to product page

Execute method


Executes a blocking method and waits for it to complete.

Type

A String value

Syntax

  • Basic
object.Execute (Command, [Prompt], [Timeout])
The Execute(object,Command,Prompt,Timeout) syntax has these parts:
objectAn expression evaluating to an object of type wodSSH.
CommandRequired. A String value. Command that will be executed.
PromptOptional. A Variant value. The prompt that we expect to receive - which indicates the end of execution. (exact text or regular expression)
TimeoutOptional. A Variant value. The time period, in seconds, in which the command must complete. (in other words, Prompt must be found in received data)

Remarks

The Execute method will send the command of your choice to the server and wait for it to complete. If it does not return within the specified time, a timeout error is generated. There is no general way of knowing what to expect in return to a command once you execute it, so you must use the Prompt argument to specify what is expected after last line of the command output. Usually, this will be your command prompt, but depending on the command that you execute, it can be something else that is meaningful to you.

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

A very common way of using the Execute method is to send a few commands sequentially, waiting for each to return something and perhaps making a decision on what to do after each. If you didn't have the Execute method you would need some complicated algorithms to do this for you. Don't forget to put CRLF (vbCrLf) sequence at the end of your command, since wodSSH will not do that for you (we can never know when you actually want it). Typically, you might want to use it like this:
 
   Ssh1.Blocking = True
   Ssh1.Connect
   Debug.Print Ssh1.WaitFor("joe@debian")
   Ssh1.Prompt = "joe@debian"
   Debug.Print Ssh1.Execute("cd /tmp" & vbLf)
   Debug.Print Ssh1.Execute("ls -al" & vbLf)
 

which will do the following - connect to the server, wait until the prompt is received (we assume here that joe@debian is the prompt that we are expecting to receive) and then change directory to /tmp and list all files it contains.

A benefit of wodSSH is that if the Execute method fails, you will still be able to Receive complete data arrived in the meantime. Received event will be fired to tell you to do that; to help you to see why it failed.

Note - this command internally disables the PromptReceived and Received events since it is taking control of what is 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 knows you are using regular expression. For example, you can use it like this:

Debug.Print Ssh1.Execute("cd /tmp" & VbLf, "regex:[\$%#>] $")

above regular expression will match when:
  • $, %, #, or > found
  • space
  • end of line
typically, our previous sample joe@somehost:-$<SPACE> does match above regex. But, hey, feel free to use something more complex such as:

      Debug.Print Ssh1.Execute("cd /tmp" & VbLf, "regex:[a-zA-z0-9]+@[a-zA-z0-9]+:[a-zA-z0-9-/]+[\$%#>] $")

      or using extended PERL syntax

      Debug.Print Ssh1.Execute("cd /tmp" & VbLf, "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. PCRE (www.pcre.org) library is used for regex support.

Platforms

Windows