Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Executing commands on a remote machine - Part 2

0.00/5 (No votes)
21 Oct 2001 1  
A simple tool that allows you to control other machines from your program

Introduction

In my previous article, I introduced a simple tool that allows you to control other machines from your program. The advantage is that the server program is written in Java so that you can run it on almost any platform. I think the tool is ideal for working within the company firewall or working at home in your one-man shop. However, my test shows that the tool does not work well with old applications on Windows 95/98. This is probably due to a limitation of Java runtime classes.

In this article, I will provide a Win32 client component in the form of a COM object that can be easily used in C++ and VB (including VB script) programs. I also included a dialog based client application that uses the com object to connect to a remote server and execute commands there.

The Win32 client component

The client component is a COM object called RemoteClient with the following simple methods: Connect, Execute, ExecuteEx, GetOutput, GetLastError and Disconnect.

The Connect method takes the strServerAddress and nServerPort arguments, it returns true if successfully connected to the server, otherwise it returns false. This method can be called multiple times with no problem.

After connecting to the server, you can call the Execute method supplying a command string and an input string as arguments. The command string is the command line (including command line arguments) for the program you want to execute on the server. The server will create a new process to execute this command. The input string you supplied will be written to the standard input stream of the created process. The return value of the Execute method indicates whether the command and the input strings are sent to the server successfully.

The ExecuteEx method is for C++ clients only. It is the same as the Execute method except that the command and the input arguments are pointers and you also need to specify the size of the data (in byte) pointed by these pointers. The advantage of using ExecuteEx is that, the input data to the command may contain null characters (it doesn't have to be a null terminated string).

The GetOutput method returns a string which represents one chunk of output data from the remote server. Because the command being executed on the remote server may not write output immediately or it may not output everything at once, the GetOutput method may block until there is some output or the command on the remote server has finished its execution. If this method returns an empty string, it means there is no more output or there is an error in the connection to the remote server.

The GetLastError method returns the last socket communication error. The Disconnect method terminates the socket connection to the remote server.

The test application

The figure above shows the test client application connecting to a server running on an NT machine.

The Connect button establishes a socket connection with the server program. The server program will check to see if the client's IP address has the proper permission, and it will either close the connection or spawn a separate thread to execute the client's requests.

The user can enter the command and its argument in the Command line field on the screen. The Input data field holds data to be written to the standard input stream of the remotely executed process. Clicking the Run button will send the command and the input to the remote server to execute.

If the Receive output box is checked, the data written to the standard output stream of the remotely executed process will be retrieved and displayed in the Output field. Note that the test application will hang until all output has been received from the remote process or the socket connection to the remote server is broken.

In this example, the command being executed is cmd, and the input data is a sequence of DOS commands. As you can see, it is possible to place the contents of a whole script file into the Input data field and execute them with one click of the Run button.

The Search output text button can be used to search a string in the Output field. If the string is found, it will be highlighted. Clicking the button again will find the next occurrence of the string.

As I have pointed out in my previous article, it is possible to launch GUI applications on the remote server, but you cannot really see the GUI from the client machine. There is a danger of starting too many processes on the remote machine. If the remote machine is running Unix, then you can use the ps and the kill commands to get rid of unwanted processes from the client.

How to build the client application

Just open the Win32Client.dsw workspace file in DevStudio. It has two projects: the COM object and the test application. Please note that, you should not build the Unicode version (it won't work properly).

By the way, the test client application also demonstrates the XYDispDriver class, an extremely simple and flexible way of using a COM object, as described in another article.

Conclusion

Thank you for reading this article. Please check out my home page for other articles and software.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here