how to excute shell script on difference machine u

2019-08-09 11:35发布

问题:

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:

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.



回答2:

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



回答3:

   // 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

}



标签: java shell