package com.pasier.xxx.util;

 import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler; public class RmtShellExecutor { private static final Logger LOG = LoggerFactory.getLogger(RmtShellExecutor.class); private Connection conn;
private String ip;
private String usr;
private String psword;
private String charset = Charset.defaultCharset().toString(); private static final int TIME_OUT = 1000 * 5 * 60; public RmtShellExecutor(String ip, String usr, String ps) {
this.ip = ip;
this.usr = usr;
this.psword = ps;
} private boolean login() throws IOException {
conn = new Connection(ip);
conn.connect();
return conn.authenticateWithPassword(usr, psword);
} public String exec(String cmds) throws IOException {
InputStream stdOut = null;
InputStream stdErr = null;
String outStr = "";
String outErr = "";
int ret = -1; try {
if (login()) {
Session session = conn.openSession();
session.execCommand(cmds);
stdOut = new StreamGobbler(session.getStdout());
outStr = processStream(stdOut, charset);
LOG.info("caijl:[INFO] outStr=" + outStr);
stdErr = new StreamGobbler(session.getStderr());
outErr = processStream(stdErr, charset);
LOG.info("caijl:[INFO] outErr=" + outErr);
session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
ret = session.getExitStatus(); } else {
LOG.error("caijl:[INFO] ssh2 login failure:" + ip);
throw new IOException("SSH2_ERR");
} } finally {
if (conn != null) {
conn.close();
}
if (stdOut != null)
stdOut.close();
if (stdErr != null)
stdErr.close();
} return outStr;
} private String processStream(InputStream in, String charset) throws IOException {
byte[] buf = new byte[1024];
StringBuilder sb = new StringBuilder();
while (in.read(buf) != -1) {
sb.append(new String(buf, charset));
}
return sb.toString();
} public static void main(String[] args) { String usr = "root";
String password = "12345";
String serverIP = "11.22.33.xx";
String shPath = "/root/ab.sh"; RmtShellExecutor exe = new RmtShellExecutor(serverIP, usr, password); String outInf; try {
outInf = exe.exec("sh " + shPath + " xn");
System.out.println("outInf= " + outInf);
} catch (IOException e) {
e.printStackTrace();
}
} }

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. [ARC068F] Solitaire [DP]

    题面 传送门 思路 单调性 首先,显然可以发现这些数在放进双端队列之后肯定是一个$V$形的排布:1在最中间,两边的数都是单调递增 那么我们拿出来的数,显然也可以划分成2个单调递减的子序列(因为我们也是 ...

  2. spring in action学习笔记十五:配置DispatcherServlet和ContextLoaderListener的几种方式。

    在spring in action中论述了:DispatcherServlet和ContextLoaderListener的关系,简言之就是DispatcherServlet是用于加载web层的组件的 ...

  3. xmlSerializer属性的使用

    学习了XmlAttribute,XmlElement属性的定义和使用. Order类定义 using System; using System.Collections.Generic; using S ...

  4. Linux : 使用 lsof 恢复文件

    用 lsof 命令在某种程度上可以恢复删除的文件, 前提是这个文件被正在运行的进程占用. 比如: 日志文件, 配置文件. lsof 恢复文件 查找需要恢复的文件和占用文件的进程 PID lsof |g ...

  5. iOS-android-windowsphone等移动终端平台开发流程图

    到了公司后,半个月时间就是在熟悉下面这张图里的流程, 项目流程图:     下面是我对这张图的一些理解:        

  6. snmp 默认团体名检测利用

    [root@izj6c2n1nth4tg8emd8h58z tmp]# lsb_release LSB Version:    :core-4.1-amd64:core-4.1-noarch[root ...

  7. picker(拖拽上下拉动的选项)

    [b]新版本更新:鼠标上下拖动选择内容:http://hiuman.iteye.com/blog/2353563[/b] (如有错敬请指点,以下是我工作中遇到并且解决的问题) 一开始搜这个内容的时候, ...

  8. VSCode配置c++环境简单教程

    VSCode配置c++环境简单教程 1.下载MinGW 安装有关gdb,gcc,g++的所有包 2.文件夹 打开一个文件夹 在里面随便写一个cpp 不管是VS还是VSCode,它的基本操作单位都是文件 ...

  9. hdu 1102(最小生成树)

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  10. 牛客小白月赛3 B 躲藏【动态规划/字符串出现cwbc子序列多少次】

    链接:https://www.nowcoder.com/acm/contest/87/B来源:牛客网 XHRlyb和她的小伙伴Cwbc在玩捉迷藏游戏 Cwbc藏在多个不区分大小写的字符串中. 好奇的X ...