5,780,524 members and growing! (80 online)
Email Password   helpLost your password?
Languages » Java » General     Intermediate

Executing commands on a remote machine - Part 1

By Xiangyang Liu 刘向阳

A simple tool that allows you to control other machines from your program
Java, Java, Dev

Posted: 11 Oct 2001
Updated: 18 Oct 2001
Views: 134,497
Bookmarked: 33 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
118 votes for this Article.
Popularity: 8.91 Rating: 4.30 out of 5
4 votes, 16.7%
1
0 votes, 0.0%
2
1 vote, 4.2%
3
4 votes, 16.7%
4
15 votes, 62.5%
5

Introduction

Sometimes, we need to execute commands on a remote machine. There are many commercial, shareware, and freeware products that can be used for this purpose. Here I present a simple Java program that can be easily modified to suit your needs. We all know the "write once, run everywhere" slogan, so the code will be working fine on any platform that has a Java Virtual Machine (JVM; but don't blame me if it breaks, I only tested it on NT and AIX). I have to warn the readers that the source code included with this article is just one example of how this can be accomplished and it has only minimal security, so use it at your own risk but it should not be hard for you to add sophisticated security.

The Java classes

The RemoteShellServer class implements the server side of the program. It first creates a server socket so that clients can connect to it. When a client is connected to the server, it will spawn a new thread using the CommandRunner class to handle the client's request. The main thread will continue to listen for new client.

The CommandRunner class implements the server thread that handles the client's request. First, it reads a command and an input to this command from the connected client. Both the command and its input are represented as byte arrays. It then creates a new process to execute the command and writes the input data to the created process. The output and error from the created processed will be read and send back to the client. The connected client may continue to send commands and input data which will be executed by this thread one by one. The server thread will not terminate if any process it created is still running, so be careful about what you are asking the server to do.

The RemoteShellClient class is an example of the client-side code. All it does is sending the command and its input provided by the user to the server and receiving the output from the server. If you want to execute commands on the remote machine from your program using this tool, you can write the client-side code based on this class. If I have time, I will provide a C++ client program with GUI for this tool later. I have actually embedded the client code within a web page (as applet) so that I can control the server machine anywhere (within the firewall, of course). This could be very dangerous, which is why the applet I put on my website is restricted to only executing certain "safe" commands on the server.

The SockData class is used to provide a way to send and receive byte arrays between the client and the server in a platform-independent way. The first 4 bytes of the data is the length of the remaining data block.

How to build and use the program

The JavaBuild.bat file contains commands to compile and bundle the Java code into a RemoteShell.jar file. You have to copy RemoteShell.jar to both machines and set the CLASSPATH environment to include this file. If you use the the provided batch files to run the server or client, I set the CLASSPATH for you.

Here are the commands to start the server program at port 50001

java RemoteShellServer 50001 207.46.230.219
java RemoteShellServer 50001 207.46.
java RemoteShellServer 50001 .microsoft.com
java RemoteShellServer 50001

The first command instructs the server to listen at port 50001 and only accepts connections from the machine with IP address 207.46.230.219. With the second command, the server only accepts connections from machines whose IP addresses contain the string 207.46. Similarly, the third command makes the server to accept only connections from machines whose addresses end with .microsoft.com. The server will accept connections from anywhere with the last command (Ouch).

To start the client, you have to specify the server IP address and port number, plus the command you want to execute on the server and its input data. For example, if you want to know what files are in the system directory of the server (assuming it is NT/Win2K), then do this:

java RemoteShellClient myserver.ipaddress.com 50001 
    "cmd" "dir c:\winnt\system32"

You will see the following output on the server console:
Client 'myclient.ipaddress.com' executed command 'cmd' with input 'dir c:\winnt\system32'

On the client console, you will see the following example output just as if you have typed dir c:\winnt\system32 on the server machine:

10/14/96  02:38a               13,584 runonce.exe
07/12/99  12:53p               54,464 s3trio3d.dll
02/01/00  02:19p               17,920 Sales.dll
10/15/98  12:04p               41,744 SAMLIB.DLL
10/15/98  12:04p              170,256 SAMSRV.DLL
10/15/98  12:04p               23,312 SAVEDUMP.EXE
10/15/98  12:04p              152,336 SCHANNEL.DLL
...

Please note that the input data to your client command can have multiple lines, so it is possible to run a script file with the cmd command (I just don't know how to type multiple lines into one line :-). If you want, you can start GUI applications on the server, but you cannot really see the GUI from the client. The following will launch notepad and wordpad on the server machine.

java RemoteShellClient myserver.ipaddress.com 50001 "notepad.exe" ""
java RemoteShellClient myserver.ipaddress.com 50001 
                                        "write.exe c:\test.txt" ""

You have to kill the instances of notepad and wordpad on the server in order to terminate the two threads in the server process. The following will launch notepad and wordpad on the server without tying up two threads in the server process:

java RemoteShellClient myserver.ipaddress.com 50001 "cmd" "notepad.exe"
java RemoteShellClient myserver.ipaddress.com 50001 "cmd" "write.exe
                                                          c:\test.txt"

Ok, that's it. Thank you for reading and please go to my homepage for more articles and programs. You may now start the constructive criticism :-)

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

About the Author

Xiangyang Liu 刘向阳



Location: United States United States

Other popular Java articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 30 (Total in Forum: 30) (Refresh)FirstPrevNext
Generalregarding the connection [modified]membersuchi_sourav21:32 17 Apr '08  
GeneralHelp Neededmemberrajeshbritto21:37 16 Oct '07  
GeneralInput Errormembergruju2:20 16 Apr '07  
QuestionCan Send file to Remote machhine using this app?memberMadhu_r7770:33 4 Jan '07  
QuestionAbout executing a process on remote machinememberankur1723:41 19 Apr '06  
QuestionDetecting application executing on remote machinememberkantnishi20:01 7 Mar '06  
AnswerRe: Detecting application executing on remote machinememberXiangyang Liu2:04 8 Mar '06  
GeneralRemoteShellServer as Windows Servicememberila146:24 7 Nov '05  
GeneralRe: RemoteShellServer as Windows ServicememberXiangyang Liu16:21 7 Nov '05  
GeneralReceiving a java exception when registering the servermembertoeho6:56 10 Jun '05  
GeneralRe: Receiving a java exception when registering the servermemberXiangyang Liu23:11 10 Jun '05  
GeneralHide Printer Icon in System TraymemberArturo41694:22 15 Jan '05  
GeneralnicesussAnonymous16:53 9 Nov '04  
GeneralExcellentmembergusano12:21 20 Sep '04  
GeneralRe: ExcellentmemberXiangyang Liu13:18 20 Sep '04  
GeneralGood Workmemberanjonny9:04 27 Apr '04  
GeneralRe: Good WorkmemberXiangyang Liu13:54 28 Apr '04  
General邀稿memberalibaba76101:10 25 Jun '03  
Generali need help badlysussalfie1:35 4 Oct '02  
GeneralDone before, but nice anywaymemberAnonymous14:21 24 Oct '01  
GeneralRe: Done before, but nice anywaymemberXiangYangLiu14:44 24 Oct '01  
GeneralRe: Done before, but nice anywaymemberXiangYangLiu14:45 24 Oct '01  
GeneralUpdate from the authormemberXiangYangLiu5:34 18 Oct '01  
GeneralPlease be professionalmemberMN3:59 16 Oct '01  
GeneralRe: Please be professionalmemberXiangYangLiu14:32 16 Oct '01  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Oct 2001
Editor: Chris Maunder
Copyright 2001 by Xiangyang Liu 刘向阳
Everything else Copyright © CodeProject, 1999-2009
Java | Advertise on the Code Project