java执行linux shell命令,并拿到返回值
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命令,并拿到返回值的更多相关文章
- linux shell自定义函数(定义、返回值、变量作用域)介绍
http://www.jb51.net/article/33899.htm linux shell自定义函数(定义.返回值.变量作用域)介绍 linux shell 可以用户定义函数,然后在shell ...
- [Python2.x] 利用commands模块执行Linux shell命令
用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态和结果,下面是commands模块的3个主要 ...
- 在docker中执行linux shell命令
在docker中执行shell命令,需要在命令前增加sh -c,例如: docker run ubuntu sh -c 'cat /data/a.txt > b.txt' 否则,指令无法被正常解 ...
- linux shell 自定义函数(定义、返回值、变量作用域)介绍
linux shell 可以用户定义函数,然后在shell脚本中可以随便调用.下面说说它的定义方法,以及调用需要注意那些事项. 一.定义shell函数(define function) 语法: [ f ...
- 转 linux shell自定义函数(定义、返回值、变量作用域)介绍
linux shell 可以用户定义函数,然后在shell脚本中可以随便调用.下面说说它的定义方法,以及调用需要注意那些事项. 一.定义shell函数(define function) 语法: [ f ...
- Java利用 ganymed-ssh2-build.jar来上传文件到linux以及下载linux文件以及执行linux shell命令
package api; import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.IOExcepti ...
- Java 执行远程主机shell命令代码
pom文件: <dependency> <groupId>org.jvnet.hudson</groupId> <artifactId>ganymed- ...
- [Python] 利用commands模块执行Linux shell命令
http://blog.csdn.net/dbanote/article/details/9414133 http://zhou123.blog.51cto.com/4355617/1312791
- java使用Runtime.exec()运行windwos dos或linux shell命令
使用Runtime.exec()运行windwos dos或linux shell命令,按实际情况具体测试 实例代码: package com.bookoo.test.command; imp ...
随机推荐
- BZOJ3990 排序(sort)
排序(sort) 题目描述 小A有一个1~2N的排列A[1..2N],他希望将数组A从小到大排序.小A可以执行的操作有N种,每种操作最多可以执行一次.对于所有的i(1<=i<=N),第i种 ...
- 【CZY选讲·次大公因数】
题目描述 给定n个数ai,求sgcd(a1,a1),sgcd(a1,a2),…,sgcd(a1,an). 其中sgcd(x,y)表示x和y的次大公因数.若不存在次大公因数,sgcd(x,y)=-1 ...
- 一键GHOST
软件简介: 一键GHOST是"DOS之家"首创的4种版本(硬盘版/光盘版/优盘版/软盘版)同步发布的启动盘,适应各种用户需要,既可独立使用,又能相互配合.主要功能包括:一键备份系统 ...
- 获得touch事件,jquery绑定事件监听器,ios设备上打开touch状态以响应focus,active等伪类
2. 默认的监听方式 document.addEventListener('touchstart', function(){ alert('hello'); }, false); 使用jquery时 ...
- Fiddler抓取HTTP请求
参考链接:http://blog.csdn.net/ohmygirl/article/details/17849983/ http://www.cnblogs.com/kingwolf_JavaScr ...
- 当表名存在 点 的时候,EntityFramework的写法
原文发布时间为:2011-05-17 -- 来源于本人的百度文章 [由搬家工具导入] 需要在 Context 中,重写 DbContext 中的 OnModelCreating 方法 进行 mappi ...
- HDU1536&&POJ2960 S-Nim(SG函数博弈)
S-Nim Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit Status ...
- 通过OpenGL ES在iOS平台实践增强现实
http://www.cnblogs.com/elvisyzhao/p/3398250.html 本文采用OpenGL ES 1固定渲染管线实现,目标为在设备拍摄到的现实世界中,绘制世界坐标轴,并根据 ...
- Linux中tty框架与uart框架之间的调用关系剖析【转】
转自:http://developer.51cto.com/art/201209/357501.htm 之前本人在"从串口驱动的移植看linux2.6内核中的驱动模型 platform de ...
- wsgi的学习(2):uWSGI的概念
uWSGI是一个Web服务器,它实现了WSGI协议.uwsgi.http等协议.Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换. 要注意 WSGI / uwsgi / u ...