CRshClient::Execute  
 
BOOL Execute(
  LPCTSTR lpszRemoteHost,  
  LPCTSTR lpszUserName,  
  LPCTSTR lpszPassword,  
  LPCTSTR lpszCommand,  
  UINT nRemotePort,  
  UINT nTimeout,  
  DWORD dwOptions  
);
BOOL Execute(
  LPCTSTR lpszRemoteHost,  
  LPCTSTR lpszUserName,  
  LPCTSTR lpszCommand,  
  UINT nRemotePort,  
  UINT nTimeout,  
  DWORD dwOptions  
);

The Execute method is used to establish a connection with the server and execute the specified command.

Parameters

lpszRemoteHost
The name of the server to connect to; this may be a fully-qualified domain name or an IP address.
lpszUserName
A null-terminated string which specifies the username used to authenticate the client session.
lpszPassword
A null-terminated string which specifies the password used to authenticate the client session.
lpszCommand
A null-terminated string which specifies the command to execute on the server.
nRemotePort
The port number the server is listening on. One of the following values should be used:
Value Description
RSH_PORT_REXEC A connection is established with the server using port 512, the rexec service. This service requires that the client provide a username and password to execute the specified command. This is the default port used when calling the version of the method which specifies a password.
RSH_PORT_RSHELL A connection is established with the server using port 514, the rshell service. This service uses host equivalence to authenticate the user. With host equivalence, the server considers the client to be equivalent to itself, and as long as the specified user exists on the server, the client is permitted to execute commands on behalf of the user without requiring a password. Host equivalence is configured by the server administrator. This is the default port used when calling the version of the method that does not specify a password.
nTimeout
The number of seconds that the client will wait for a response from the server before failing the current operation.
dwOptions
An unsigned integer that specifies one or more options. This parameter is constructed by using a bitwise operator with any of the following values:
Value Description
RSH_OPTION_RESERVEDPORT This option specifies that a reserved port should be used to establish the connection. Reserved ports are those port numbers which are less than 1024. This option should be specified when connecting on the RSH_PORT_RSHELL port.
RSH_OPTION_PREFER_IPV6 This option specifies the client should prefer the use of IPv6 if the server hostname can be resolved to both an IPv6 and IPv4 address. This option is ignored if the local system does not have IPv6 enabled, or when the hostname can only be resolved to an IPv4 address. If the server hostname can only be resolved to an IPv6 address, the client will attempt to establish a connection using IPv6 regardless if this option has been specified.
RSH_OPTION_FREETHREAD This option specifies that this instance of the class may be used by any thread, and is not limited to the thread which created it. The application is responsible for ensuring that access to the class instance is synchronized across multiple threads.

Return Value

If the method succeeds, the return value non-zero. If the method fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

This method will block the current thread until a connection has been established or the timeout period has elapsed. To prevent it from blocking the main user interface thread, the application should create a background worker thread and establish a connection by calling the Connect method in that thread. If the application requires multiple simultaneous connections, it is recommended you create a worker thread for each connection.

The dwOptions argument can be used to specify the threading model that is used by the library when a connection is established. By default, the class instance is initially attached to the thread that created it. From that point on, until the handle is released, only the owner may call methods using that instance of the class. The ownership of the class instance may be transferred from one thread to another using the AttachThread method.

Specifying the POP_OPTION_FREETHREAD option enables any thread to call any method in that instance of the class, regardless of which thread created it. It is important to note that this option disables certain internal safety checks which are performed by the library and may result in unexpected behavior unless access to the class instance is synchronized. If one thread calls a function in the library, it must ensure that no other thread will call another function at the same time using the same instance.

Example

The following example demonstrates connecting to a server, executing a command on the server, reading the output and storing it in a string. This uses the rexec service on port 512, and requires a username and password.

CRshClient rshClient;
BOOL bConnected;

// Establish a connection with the server and execute
// the command as the specified user
bConnected = rshClient.Execute(strHostName,
                               strUserName,
                               strPassword,
                               strCommand);

// If the connection could not be established or the command
// could not be executed, display an error message
if (bConnected == FALSE)
{
    rshClient.ShowError();
    return;
}

CString strBuffer;
CString strResult;
INT nResult;

// Read the output from the command and store it in the strResult
// string; note that a UNIX server terminates each line of output
// with a bare linefeed, which is something that should be kept
// in mind when parsing the string
do
{
    if ((nResult = rshClient.Read(strBuffer)) > 0)
        strResult += strBuffer;
}
while (nResult > 0);

// If there was an error reading the output from the server, then
// display the error and disconnect
if (nResult == RSH_ERROR)
{
    rshClient.ShowError();
    rshClient.Disconnect();
    return;
}

rshClient.Disconnect();

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: csrshv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also