• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HOW TO RUN COMMAND ON REMOTE PC

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Searching for "telnet" in your java books.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or perhaps RMI?
bear
 
RUI CHEN
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you just want to use native methods read up on JNI
 
RUI CHEN
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Bear Bibeault
What's RMI?
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remote Method Invocation. See books like Core Java II vol 2 (advanced features) by Cay Horstmann and Gary Cornell, older editions only e.g. 6/e (2005). I thought people had stopped using RMI; remember you have stumbled onto an 18½‑year‑old thread.

And . . . welcome to the Ranch
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Remote Method Invocation. See books like Core Java II vol 2 (advanced features) by Cay Horstmann and Gary Cornell, older editions only e.g. 6/e (2005). I thought people had stopped using RMI; remember you have stumbled onto an 18½‑year‑old thread.

And . . . welcome to the Ranch



RMI-IIOP is the Internet-friendly protocol for Enterprise JavaBeans. Assuming that anyone is still using remote EJBs.


The virtue of RMI, incidentally, is that it permits a long-term connection between client and server passing data back and forth as a Java Serialized data stream, allowing for minimal overhead.

The disadvantage of RMI is that A) since the format of serialized Java objects is not a public specification and therefore is not usable unless both client and server are Java apps (unlike CORBA, which is very definitely dead). Furthermore, the exact format of serialized data isn't constant between JVM versions, so the whole setup is quite fragile and best only used in-house where both client and server are undet unified control.

On the whole, it's better to use either Web Services or MQ/JMS messaging protocols these days.
 
reply
    Bookmark Topic Watch Topic
  • New Topic