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. }

java执行linux shell命令,并拿到返回值的更多相关文章

  1. linux shell自定义函数(定义、返回值、变量作用域)介绍

    http://www.jb51.net/article/33899.htm linux shell自定义函数(定义.返回值.变量作用域)介绍 linux shell 可以用户定义函数,然后在shell ...

  2. [Python2.x] 利用commands模块执行Linux shell命令

    用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态和结果,下面是commands模块的3个主要 ...

  3. 在docker中执行linux shell命令

    在docker中执行shell命令,需要在命令前增加sh -c,例如: docker run ubuntu sh -c 'cat /data/a.txt > b.txt' 否则,指令无法被正常解 ...

  4. linux shell 自定义函数(定义、返回值、变量作用域)介绍

    linux shell 可以用户定义函数,然后在shell脚本中可以随便调用.下面说说它的定义方法,以及调用需要注意那些事项. 一.定义shell函数(define function) 语法: [ f ...

  5. 转 linux shell自定义函数(定义、返回值、变量作用域)介绍

    linux shell 可以用户定义函数,然后在shell脚本中可以随便调用.下面说说它的定义方法,以及调用需要注意那些事项. 一.定义shell函数(define function) 语法: [ f ...

  6. Java利用 ganymed-ssh2-build.jar来上传文件到linux以及下载linux文件以及执行linux shell命令

    package api; import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.IOExcepti ...

  7. Java 执行远程主机shell命令代码

    pom文件: <dependency> <groupId>org.jvnet.hudson</groupId> <artifactId>ganymed- ...

  8. [Python] 利用commands模块执行Linux shell命令

    http://blog.csdn.net/dbanote/article/details/9414133 http://zhou123.blog.51cto.com/4355617/1312791

  9. java使用Runtime.exec()运行windwos dos或linux shell命令

    使用Runtime.exec()运行windwos dos或linux shell命令,按实际情况具体测试     实例代码: package com.bookoo.test.command; imp ...

随机推荐

  1. GYM - 101147 B.Street

    题意: 大矩形代表市场,大矩形当中有很多小矩形样式的伞.这些小矩形都贴着大矩形的左边或者右边且互不相交.小矩形以外的地方都是阳光.求经过大矩形时在阳光下的最短时间. 题解: 最短路的做法.起点和终点与 ...

  2. 电阻 (resistance)

    电阻 (resistance) 题目描述 每次小x物理作业没做完时,总是会去和老师交流感情,他们之间由此建立起来良好的师生关系.于是有一天,老师带着一道物理难题来见小x. 这道题给出了一个有n个电阻的 ...

  3. linux命令Netstat

    1.需求 了解Netstat命令 2.简介 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multi ...

  4. Android 自定义动画 Loading

    转自:http://my.oschina.net/janson2013/blog/118558 1.定义一个ImageView 定义一个ImageView是为了装载图片,其中的图片将被rotate用来 ...

  5. hihoCoder 1015KMP

    #include <iostream> #include <algorithm> #include <stdio.h> #include <math.h> ...

  6. SQL联合查询(内联、左联、右联、全联)语法

    SQL联合查询(内联.左联.右联.全联)语法 概述: 联合查询效率较高,举例子来说明联合查询:内联inner join .左联left outer join .右联right outer join  ...

  7. 关于java1.7集合源码阅读

    工作中每天都会和java集合打交道,虽然以前也看过jdk源码的实现,但有些东西时间长了还是会遗忘,或者有些实现在新版本中有了新的变化,俗话说"温故而知新",所以打算再阅读一下相关源 ...

  8. Fiddler抓包6-get请求(url详解)【转载】

    本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/fiddler/ 前言 上一篇介绍了Composer的功能,可以模拟get和post请求 ...

  9. blog postman 翻译(2):postman中新增request attributes 的描述(desc)

    blog的原文链接是:http://blog.getpostman.com/2017/06/03/descriptions-for-request-attributes-in-postman/ pos ...

  10. 华农oj 2192: hzk又在打人【CRT合并/待补】

    2192: hzk又在打人 Time Limit: 12 Sec Memory Limit: 512 MB Submit: 52 Solved: 1 [Submit][Status][Web Boar ...