码迷,mamicode.com
首页 > 编程语言 > 详细

java执行linux shell命令,并拿到返回值

时间:2014-06-18 08:47:32      阅读:476      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   color   

  1 package com.suning.spc.util;
  2 
  3 import java.io.IOException;
  4 import java.io.InputStream;
  5 import java.nio.charset.Charset;
  6 
  7 import org.slf4j.Logger;
  8 import org.slf4j.LoggerFactory;
  9 
 10 import ch.ethz.ssh2.ChannelCondition;
 11 import ch.ethz.ssh2.Connection;
 12 import ch.ethz.ssh2.Session;
 13 import ch.ethz.ssh2.StreamGobbler;
 14 
 15 public class RmtShellExecutor {
 16 
 17     private static final Logger LOG = LoggerFactory.getLogger(RmtShellExecutor.class);
 18 
 19     private Connection conn;
 20     private String ip;
 21     private String usr;
 22     private String psword;
 23     private String charset = Charset.defaultCharset().toString();
 24 
 25     private static final int TIME_OUT = 1000 * 5 * 60;
 26 
 27     public RmtShellExecutor(String ip, String usr, String ps) {
 28         this.ip = ip;
 29         this.usr = usr;
 30         this.psword = ps;
 31     }
 32 
 33     private boolean login() throws IOException {
 34         conn = new Connection(ip);
 35         conn.connect();
 36         return conn.authenticateWithPassword(usr, psword);
 37     }
 38 
 39     public String exec(String cmds) throws IOException {
 40         InputStream stdOut = null;
 41         InputStream stdErr = null;
 42         String outStr = "";
 43         String outErr = "";
 44         int ret = -1;
 45 
 46         try {
 47             if (login()) {
 48                 Session session = conn.openSession();
 49                 session.execCommand(cmds);
 50                 stdOut = new StreamGobbler(session.getStdout());
 51                 outStr = processStream(stdOut, charset);
 52                 LOG.info("caijl:[INFO] outStr=" + outStr);
 53                 stdErr = new StreamGobbler(session.getStderr());
 54                 outErr = processStream(stdErr, charset);
 55                 LOG.info("caijl:[INFO] outErr=" + outErr);
 56                 session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
 57                 ret = session.getExitStatus();
 58 
 59             } else {
 60                 LOG.error("caijl:[INFO] ssh2 login failure:" + ip);
 61                 throw new IOException("SSH2_ERR");
 62             }
 63 
 64         } finally {
 65             if (conn != null) {
 66                 conn.close();
 67             }
 68             if (stdOut != null)
 69                 stdOut.close();
 70             if (stdErr != null)
 71                 stdErr.close();
 72         }
 73 
 74         return outStr;
 75     }
 76 
 77     private String processStream(InputStream in, String charset) throws IOException {
 78         byte[] buf = new byte[1024];
 79         StringBuilder sb = new StringBuilder();
 80         while (in.read(buf) != -1) {
 81             sb.append(new String(buf, charset));
 82         }
 83         return sb.toString();
 84     }
 85 
 86     public static void main(String[] args) {
 87 
 88         String usr = "root";
 89         String password = "12345";
 90         String serverIP = "11.22.33.xx";
 91         String shPath = "/root/ab.sh";
 92 
 93         RmtShellExecutor exe = new RmtShellExecutor(serverIP, usr, password);
 94 
 95         String outInf;
 96 
 97         try {
 98             outInf = exe.exec("sh " + shPath + " xn");
 99             System.out.println("outInf= " + outInf);
100         } catch (IOException e) {
101             e.printStackTrace();
102         }
103     }
104 
105 }

 

java执行linux shell命令,并拿到返回值,布布扣,bubuko.com

java执行linux shell命令,并拿到返回值

标签:style   class   blog   code   java   color   

原文地址:http://www.cnblogs.com/enshrineZither/p/3793459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!