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. 洛谷 P4514 上帝造题的七分钟 解题报告

    P4514 上帝造题的七分钟 题目背景 裸体(裸题)就意味着身体(神题). 题目描述 "第一分钟,X说,要有矩阵,于是便有了一个里面写满了\(0\)的\(n \times m\)矩阵. 第二 ...

  2. vue数组对象修改触发视图更新

    直接修改数组元素是无法触发视图更新的,如 this.array[0] = { name: 'meng', age: 22 } 修改array的length也无法触发视图更新,如 this.array. ...

  3. 【CZY选讲·吃东西】

    题目描述 一个神秘的村庄里有4家美食店.这四家店分别有A,B,C,D种不同的美食.LYK想在每一家店都吃其中一种美食.每种美食需要吃的时间可能是不一样的.现在给定第1家店A种不同的美食所需要吃的时间 ...

  4. Sublime Text3配置SublimeREPL快捷键的方法(Python)

    因为用sublime运行python,如果有input()函数,ctrl+b是不能输入数据的,所以下载安装了sublimeREPL进行调试. 但是sublimeREPL没有自定义快捷键,所以只有自己设 ...

  5. Html5学习进阶四 表单元素和表单属性

    HTML5 的新的表单元素: HTML5 拥有若干涉及表单的元素和属性. 本章介绍以下新的表单元素: datalist keygen output 浏览器支持 Input type IE Firefo ...

  6. 百度经验:Win10查看已存储WiFi密码的两种方法

    方法一:网络和共享中心查询 具体步骤可以参考:Win10查看WIFI密码的方法 方法二:命令提示符查询 1.右键单击开始按钮,选择“命令提示符(管理员)” 2.输入如下命令(下图①号命令): nets ...

  7. switch与if 性能测试

    测试结果:switch性能更高. 测试过程:新建一个Win32 Console Application, 在cpp文件中添加下面代码 #include "stdafx.h" #in ...

  8. Windows基础-实时录音程序(WaveXXX)

    写在前面 一开始是打算用这个老接口做讯飞语音识别的程序,在转移到UWP时发现,这玩意在Windows Runtime中屏蔽(弃用)了,将来会更新使用WASAPI的程序 WaveRecorder类代码下 ...

  9. 5.DataFrame(基本概念)

  10. flask框架下的jinja2模板引擎(3)(模板继承与可以在模板使用的变量、方法)

    flask 框架下的jinja2模块引擎(1):https://www.cnblogs.com/chichung/p/9774556.html flask 框架下的jinja2模块引擎(2):http ...