*
The moose likes Java in General and the fly likes HOW TO RUN COMMAND ON REMOTE PC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "HOW TO RUN COMMAND ON REMOTE PC" Watch "HOW TO RUN COMMAND ON REMOTE PC" New topic
Author

HOW TO RUN COMMAND ON REMOTE PC

RUI CHEN
Greenhorn

Joined: Nov 16, 2002
Posts: 12
Hi there,
I wanna know how to write a program which can execute command for me on remote computer, and then return the result.
For example, on the local machine, I start the program and it connects to the remote unix machine. then, i could type "whoami", and the program will execute the same command in the remote computer, return the result to me.
Thank you very much!


Life is like a .jar, so many many things contained.
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
Searching for "telnet" in your java books.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
I am a bit unclear if you are looking for something like TelNet or FTP that invokes an interactive session on the remote machine, or if you are looking to have part of your application on your machine and part of it on the remote machine and use RMI to interact between them.
Or perhaps you are talking about a servlet or J2EE set up where you invoke the application from your browser but it runs remotely.
Can you explain what you are trying to accomplish a bit more?


"JavaRanch, where the deer and the Certified play" - David O'Meara
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

Or perhaps RMI?
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
RUI CHEN
Greenhorn

Joined: Nov 16, 2002
Posts: 12
Hi guys,
Sorry for my poor expression.
It should have one part running in the server, and one part running in a client computer. The server response to the client.
Users would be able to run command on the remote machine, on which the server part resides. And server sends back the result to the client.
From the point of view of the users, they are running a telnet.
So, in my opinion, the server part should invoke native method. That's the part I am not familiar.
As to the Mr Liu, could you please tell me where to find that part of API? i checked the documentation, but found nothing
Thanks all you guys, looking to learn more from you!
Rui
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
You don't need telnet.
What you are describing is probably RMI (but could be done with J2EE also).
We have a forum for Distributed Java that includes RMI. Over there Michael Ernest ran a small seminar in his thread RMI from start to finish .
Try reading that and see if that is what you are looking for.
RUI CHEN
Greenhorn

Joined: Nov 16, 2002
Posts: 12
RMI may not the thing I am looking for. I need be familiar with native methods.
I think i should find out a telent program written in Java, then I could learn from it.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Telnet is a program that lets you invoke a session on another machine. Like opening a DOS window - only it would be a unix window or whatever. It is not intended to interface with an application. You would need to do keystroke emulation or something to accomplish that, if I understand what you are trying to accomplish. It has nothing to do with native methods.
If you are truely looking just to open a Unix window interacting with the remote machine, do a google search on TelNet. There are some free versions.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
If you just want to use native methods read up on JNI
RUI CHEN
Greenhorn

Joined: Nov 16, 2002
Posts: 12
Hi Cindy,
Thanks for your reply!
I want the program to work in client/server style. Particullary, the server part works on Unix and will carry out the unix commands the client sends. It returns the result to the client, so the users on the client seems that they could interact with the remote unix machine.
So,in my opinion,the most important thing is how to make the server part acrry out the commands on that remote machine, which it resides on.
If we request "ls", it may be easy to list the file and folders on the remote machine, and return the result. However, if the request is things like "whoami", then, Java certainly does not provide any methods to carry out this kind of unix commands.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Rui,
What you are trying to do is similar to the old Unix rcmd or rsh. Look into the Runtime class. It has a variety of exec() methods similar to the C standard library exec* functions. There are some caveats as I recall in getting the results back from the process' stdout due to an implementation bug but there are known workarounds.
Now if you want to try it with GUI's then the Robot class is where you want to go. It has everything necessary to grab the mouse, generate a screen shot, etc.
Both of these classes are subject to security checks, so on a Unix box your server will probably have to be set uid root.
Hope this helps,
Michael Morris
[ February 17, 2003: Message edited by: Michael Morris ]
[ February 17, 2003: Message edited by: Michael Morris ]

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Does your client side have to be able to know what went on on the server side?
I mean, you can use the Runtime.exec() to have your java client invoke a telnet session and the user can interact with the remote server all that they want, but your client is not going to know what happened while they were interacting with it. Is that OK?
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Rui,
I think something like the following is what you are looking to do:
RSHServer.java

RSHClient.java

Here are the client results on my machine (Windows 2000 Pro):

This is a very simplistic approach and will need a great deal of work for the server to be truly functional. For one thing, you'll also need to check the process' error stream. You'll also probably want to return the process exit value. All this means is you will most likely need to create a class ProcessResult which implements Serializable and is used to hold the exit value and stdout and stderr results. Then you send it back to the client with an ObjectOutputStream.
Hope this helps,
Michael Morris
RUI CHEN
Greenhorn

Joined: Nov 16, 2002
Posts: 12
Hi everybody,
Yeah, Runtime.exec() is what I am lookin for!
All your guys are so kind to me, especially Michael and Cindy.
Your kind help is deeply apprecaited!
Thanks so much!!!

Sincerely yours
Rui
Sergi Bradley
Greenhorn

Joined: Feb 19, 2003
Posts: 12
Here take a look at my code..i can run commands but am having trouble running mplayer on linux. I got two threads for in and error...but the output stream and stalling when u insert parameters to the process is giving me trouble.
This should for u on simple commands with no interaction. Just construct a string[] that looks like this:
String[] command = {"mplayer", "ragetool.mp3"};
exe.execute(command);
it should print to your screen.
here is the link: Runtime exe, that is in this forum as we speak
[ February 20, 2003: Message edited by: Sergi Bradley ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HOW TO RUN COMMAND ON REMOTE PC
 
Similar Threads
how remote monitoring works -- without any agents at remote machine
Is it possible to execute commands on remote machine
plz help me
running a shell script in java
Callling a Script from a java program