java執行linux shell命令,並拿到傳回值__java

來源:互聯網
上載者:User
原文:http://www.cnblogs.com/enshrineZither/p/3793459.html
  1 package com.pasier.xxx.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 }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.