Execute Method  
 

Execute a command on the server and return the output.

Syntax

object.Execute( [RemoteHost], [RemotePort], [UserName], [Password], [Command], [Timeout], [Options] )

Parameters

RemoteHost
A string which specifies the host name or IP address of the server. If this argument is not specified, it defaults to the value of the HostAddress property if it is defined. Otherwise, it defaults to the value of the HostName property.
RemotePort
A number which specifies the port to connect to on the server. If this argument is not specified, it defaults to the value of the RemotePort property. A value of zero indicates that the default port number for this service should be used to establish the connection.
UserName
A string which specifies the user name which will be used to authenticate the client session. If this argument is not specified, it defaults to the value of UserName property.
Password
A string which specifies the password which will be used to authenticate the client session. If this argument is not specified, it defaults to the value of the Password property.
Command
A string which specifies the command that will be executed on the server. If this argument is not specified, it defaults to the value of the Command property.
Timeout
The number of seconds that the client will wait for a response before failing the operation. If this argument is not specified, the value of the Timeout property will be used as the default.
Options
A numeric value which specifies one or more options. If this argument is omitted or a value of zero is specified, a default connection will be established. This argument is constructed by using a bitwise operator with any of the following values:
Value Constant Description
0 sshOptionNone No options specified. A standard terminal session will be established with the default terminal type.
1 sshOptionKeepAlive This option specifies the library should attempt to maintain an idle client session for long periods of time. This option is only necessary if you expect that the connection will be held open for more than two hours. This option is the same as setting the KeepAlive property to a value of true.
2 sshOptionNoPTY This option specifies that a pseudoterminal (PTY) should not be created for the client session. This option is automatically set if the Command property specifies a command to be executed on the server.
4 sshOptionNoShell This option specifies that a command shell should not be used when executing a command on the server.
8 sshOptionNoAuthRSA This option specifies that RSA authentication should not be used with SSH-1 connections. This option is ignored with SSH-2 connections and should only be specified if required by the server.
16 sshOptionNoPwdNul This option specifies the user password cannot be terminated with a null character. This option is ignored with SSH-2 connections and should only be specified if required by the server.
32 sshOptionNoRekey This option specifies the client should never attempt a repeat key exchange with the server. Some SSH servers do not support rekeying the session, and this can cause the client to become non-responsive or abort the connection after being connected for an hour.
64 sshOptionCompatSID This compatibility option changes how the session ID is handled during public key authentication with older SSH servers. This option should only be specified when connecting to servers that use OpenSSH 2.2.0 or earlier versions.
128 sshOptionCompatHMAC This compatibility option changes how the HMAC authentication codes are generated. This option should only be specified when connecting to servers that use OpenSSH 2.2.0 or earlier versions.

Return Value

A string that contains the output of the command that was executed on the server. To get the exit code returned by the program, check the value of the ExitCode property. If an empty string is returned, this indicates that there was either no data available, or an error has occurred and the LastError property will return a non-zero value.

Remarks

This method establishes a network connection with a server and executes the specified command. The output from the command is returned as a string. This method should not be used if the connection to the server must be established through a proxy server. If the connection must be made through a proxy server, then you should set the Command property to the specify the command to execute, call the Connect method to establish the connection, and then use either the Read or ReadLine methods to read the output.

When the command output is being read from the server, this method will automatically convert the data to match the end-of-line convention used on the Windows platform. This is useful when executing a command on a UNIX based system where the end-of-line is indicated by a single linefeed, while on Windows it is a carriage-return and linefeed pair. If the output contains embedded nulls or escape sequences, then this conversion will not be performed.

Example

The following example demonstrates how to use the Execute method and check for an error condition:

Dim strOutput As String

SshClient1.HostName = strServerName
SshClient1.UserName = strUserName
SshClient1.Password = strPassword
SshClient1.Command = "/bin/ls -l"

strOutput = SshClient1.Execute()

If Len(strOutput) = 0 Then
    If SshClient1.LastError > 0 Then
        MsgBox SshClient1.LastErrorString, vbExclamation
        Exit Sub
    End If
End If

See Also

Command Property, ExitCode Property, LastError Property, Connect Method