This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Execution of commands on remote machine. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Execution of commands on remote machine." Watch "Execution of commands on remote machine." New topic
Author

Execution of commands on remote machine.

Shivam Choubey
Greenhorn

Joined: Oct 31, 2008
Posts: 2
Hello All,

I have a requirment in which we need to execute command on a remote machine.
Do any one know how to do in a simplest way.
We are short of time so code snippents will work wonders .

Thanks & regards,
Shivam
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hello Shivam Choubey ,

Welcome to JavaRanch

Though it is impossible to give the complete code snippet, an idea can very well be given as the implementation is left to you and the language you choose.

One idea is you can pass the command from your machine to the remote host and get the command executed in the remote machine. Socket Programming is the best rescue for the same.

Any other easy way ??


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

If the remote machine is a Unix machine with a running SSH daemon you can use SSH to execute the command. There are enough Java SSH solutions around the Internet, Google can help you with those.

If it's a Windows machine perhaps RPC or something similar can help you, but that's outside my scope of knowledge.


Raghavan: a Socket can help you if you have your own program / service running on the remote machine with a ServerSocket listening for requests, but otherwise you would need to connect with another service that allows command execution. There aren't many of those.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Rob Prime:


Raghavan: a Socket can help you if you have your own program / service running on the remote machine with a ServerSocket listening for requests, but otherwise you would need to connect with another service that allows command execution. There aren't many of those.


Thank you Rob. Yes, SSH is indeed a good option. Somehow I missed it

As you said, "There aren't many of those" -- the default option was of Sockets!
Shivam Choubey
Greenhorn

Joined: Oct 31, 2008
Posts: 2
Thanks Raghav & Rob for helping... I will check with it.

Regards,
Shivam
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Originally posted by Raghavan Muthu:
As you said, "There aren't many of those" -- the default option was of Sockets!

But with a Socket you can only connect to a listening server socket, and the application in which that server socket is running must support the protocol: reading a command, then executing that.

The SSH daemon is one such applcation (given the right parameters), but its protocol has some more caveats. That's why a simple Socket won't do. A Java SSH library will probably use Sockets in the background, but with a lot of extra code added.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Rob Prime:

But with a Socket you can only connect to a listening server socket, and the application in which that server socket is running must support the protocol: reading a command, then executing that.



True. I meant you get connected to a listening socket and pass the commands to the socket as data. The listening socket then has to delegate this passed command and get it executed in its native OS and in turn returns back the actual result of the command from the OS.

If my understanding is right, this is what generally happens behind the scenes even in case of SSH.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
If you are short of time, it would probably help to provide more information (what systems are we talking about, how are they connected, what commands do you need to execute and why, how much control do you have over those systems etc. pp.) so that we can be more effective helping you.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
Consider Paul's rocket mass heater.
 
subject: Execution of commands on remote machine.
 
Similar Threads
when an objet is called from a remote place??and when that object is not loaded..
Reading and Writing Files In Ejb
Help Needed in Applets....Urgent
getResource in ServletContext
Can we Configure Multiple JBoss Instances On One Machine?