开发者

how to excute shell script on difference machine using java

开发者 https://www.devze.com 2023-01-13 09:50 出处:网络
i want java code example for excute shell script on difference machine (from windows OS to Unix).Help me pls.

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)


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.


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


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

}

0

精彩评论

暂无评论...
验证码 换一张
取 消