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

举报
风吹稻花香 发表于 2021/06/05 01:55:05 2021/06/05
【摘要】 原文: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.charse...
原文: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 }

文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/73647984

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。