views:

52

answers:

3

Hi All, i want java code example for excute shell script on difference machine (from windows OS to Unix).Help me pls.

Sorry for my question unclear.i have 2 use case for this question :

case 1 : Different Machine

case 2 : Different OS (because first machine is windows 2003 server os and remote machine is unix)

+1  A: 

Considering that kind of question, my reference is a JavaWorld article : When runtime.exec won't.

Riduidel
That is all about running an external program on the same machine.
David Dorward
+4  A: 

You will (probably) want to use a SSH library (I believe JSch is popular) to create a ssh connection to the machine via Java code, and then simply run the script that you want to run on the machine that you are ssh'd into. This is assuming the script you want to run is on the remote machine. If it's local then I'd probably just copy it over first before running it... or re-write the script to do the ssh-ing itself.

Stephen
A: 
   // Maximize portability by looking up the name of the command to execute
   // in a configuration file. 
   java.util.Properties config;  
   String cmd = config.getProperty("sysloadcmd");
   if (cmd != null) {
  // Execute the command; Process p represents the running command
   Process p = Runtime.getRuntime().exec(cmd);         // Start the command
  InputStream pin = p.getInputStream();               // Read bytes from it
  InputStreamReader cin = new InputStreamReader(pin); // Convert them to chars
  BufferedReader in = new BufferedReader(cin);        // Read lines of chars
  String load = in.readLine();                        // Get the command output
  in.close();                                         // Close the stream

}

Rupeshit
And what about this connects to the external machine? What protocol does it use?
David Dorward
thank you for answer.
MewZ