File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Is it possible to execute commands on remote machine Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is it possible to execute commands on remote machine" Watch "Is it possible to execute commands on remote machine" New topic
Author

Is it possible to execute commands on remote machine

Harish Tiruvile
Ranch Hand

Joined: Dec 01, 2005
Posts: 99
HI all,

Qusteion:
Is it possible to write an application which will execute commands on remote machine...like restating tomcat server

Now i want to execute command on remote machine , so

1. password authentication
2.restarting server(if authenticated)


By using java Runtime class i can execute commands on local machines...Is it possible to excute commands and process the result on remote machine?


Giving up is the easiest thing in the world to do..but holding it together when everything seems like falling apart is true strength!!
with regards, Harish.T
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Sure. All you need is a program running on the remote machine listening for commands to execute, and a way to communicate with that program. Lots of UNIX machines already have such a program running: it's called sshd. I suppose lots of Windows machines have such a program too; it's called Internet Explorer (baboom BOOM kish! Thanks folks, don't forget to tip your waitress!)


[Jess in Action][AskingGoodQuestions]
Harish Tiruvile
Ranch Hand

Joined: Dec 01, 2005
Posts: 99
Thank you Ernest Friedman-Hill ,

Can you please tell me How to proceed..i mean which class in java provide SSH connection..is it possible to do that using Runtime class..if you have any link please post that site here
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

If your servers are already running ssh, and you'll have ssh access, then there are a lot of Java SSH libraries out there to choose from.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Ernest Friedman-Hill:
Sure. All you need is a program running on the remote machine listening for commands to execute, and a way to communicate with that program. Lots of UNIX machines already have such a program running: it's called sshd. I suppose lots of Windows machines have such a program too; it's called Internet Explorer (baboom BOOM kish! Thanks folks, don't forget to tip your waitress!)


Nice one


Tony Morris
Java Q&A (FAQ, Trivia)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41856
    
  63
If it's specifically Tomcat you want to control from afar, then you can do that via a number of Ant tasks that support that. Here is some documentation about that.


Ping & DNS - my free Android networking tools app
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by Ernest Friedman-Hill:
Sure. All you need is a program running on the remote machine listening for commands to execute, and a way to communicate with that program. Lots of UNIX machines already have such a program running: it's called sshd. I suppose lots of Windows machines have such a program too; it's called Internet Explorer (baboom BOOM kish! Thanks folks, don't forget to tip your waitress!)




The documented way to do it would be to use Windows Scripting Host ability to call remote scripts.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

Hello,

Start with Server Sockets & Normal Sockets ... seems you may require RAW Sockets too. First try to develop the server socket on local machine and make sure that it is working fine then port this to remote machine and try.

1) The server socket will get the request from remote client and will perform the operation and will return back the result.

2) Remote java program will process the data and will show to the end user.

If you want to protect the data then use any wrapper over socket.

All the best
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Originally posted by Srinivas Kalvala:

Start with Server Sockets & Normal Sockets ... seems you may require RAW Sockets too.


No; sorry, but this is a terrible idea. If he's going to roll his own solution, better to use either HTTP(S) or RMI. It would silly to start absolutely from scratch for this -- what would be the point?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Originally posted by Ulf Dittmer:
If it's specifically Tomcat you want to control from afar, then you can do that via a number of Ant tasks that support that.


You can, although this mechanism falls down if (when) you need to restart an utterly frozen Tomcat -- which does happen -- because it uses Tomcat's built-in admin server. If you're local, you can kill it and restart it.
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

Originally posted by Ernest Friedman-Hill:


It would silly to start absolutely from scratch for this -- what would be the point?



Sorry, I did not get your point and I dont think it as writing from scratch. Already java sockets are providing enough abstraction. Do you want to use any other specific wrapper over Socket for this operation?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Originally posted by Srinivas Kalvala:
Do you want to use any other specific wrapper over Socket for this operation?


Did you read any of the other posts in this thread (about ssh, or wsh?) or even the post you're responding to (about RMI or HTTPS?)
Robert Hill
Ranch Hand

Joined: Feb 24, 2006
Posts: 94
I think a good(ie more flexible) solution would be to implement the telnet protocol(RFC 854) and add in ssh. Something similar to the open source program putty. http://www.chiark.greenend.org.uk/~sgtatham/putty/

It would work for starting tomcat and any other program on the remote system, provided you have permission to do so of course.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
This stuff was invented before I was born and here we are reinventing it - perhaps for good reason. Do the existing solutions actually target the problem and nothing more? If so, why reinvent since you will have the exact same product at the end? If not, then reinvent it (make the wheel more round). More often than not, I see a reinvention without the requirement leak. In other words, a minimalism of what already exists. This is also an inherent property of Java/OO itself.

Why aren't alarm bells ringing for anyone else? Backus wrote about it the year I was born!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible to execute commands on remote machine