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 ...
随机推荐
- 2017 多校4 Security Check
2017 多校4 Security Check 题意: 有\(A_i\)和\(B_i\)两个长度为\(n\)的队列过安检,当\(|A_i-B_j|>K\)的时候, \(A_i和B_j\)是可以同 ...
- BZOJ[Sdoi2014]数表 莫比乌斯反演
[Sdoi2014]数表 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 2383 Solved: 1229[Submit][Status][Disc ...
- oracle查询时遇到的坑
select * from manu_routecard t left join manu_routecardlist mr on t.routecard_id = mr.routecard_id l ...
- python 读取数据库时,datetime类型无法被json序列化--解决方案
新增针对datetime的jsonencode: # -*- coding: utf-8 -*- import json from datetime import date, datetime cla ...
- js函数调用与声明 (for时注意)
可以的: test(); // 直接function 方式声明的函数可以直接调用,后声明 function test(){} aa(); //error var 方式声明的函数需先声明后调用v ...
- 洛谷P3120 [USACO15FEB]Cow Hopscotch
题目描述 Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented ...
- UVA 10652 Board Wrapping(二维凸包)
传送门 刘汝佳<算法竞赛入门经典>P272例题6包装木板 题意:有n块矩形木板,你的任务是用一个面积尽量小的凸多边形把它们抱起来,并计算出木板占整个包装面积的百分比. 输入:t组数据,每组 ...
- python接口自动化10-token登录【转载】
本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/python%E6%8E%A5%E5%8F%A3%E8%87%AA%E5%8A%A8%E ...
- windows7_64位安装sql_server_2000成功
对于4合一的版本来说只要找到安装包里的 STANDARD.PERSONAL.ENTERPRISE.DEVELOPER文件夹下的 X86\SETUP\SETUPSQL.EXE 任何一个运行即可,(理论上 ...
- ELK之收集日志到mysql数据库
写入数据库的目的是持久化保存重要数据,比如状态码.客户端浏览器版本等,用于后期按月做数据统计等. 环境准备 linux-elk1:10.0.0.22,Kibana ES Logstash Nginx ...