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 ...
随机推荐
- GYM - 101147 B.Street
题意: 大矩形代表市场,大矩形当中有很多小矩形样式的伞.这些小矩形都贴着大矩形的左边或者右边且互不相交.小矩形以外的地方都是阳光.求经过大矩形时在阳光下的最短时间. 题解: 最短路的做法.起点和终点与 ...
- 电阻 (resistance)
电阻 (resistance) 题目描述 每次小x物理作业没做完时,总是会去和老师交流感情,他们之间由此建立起来良好的师生关系.于是有一天,老师带着一道物理难题来见小x. 这道题给出了一个有n个电阻的 ...
- linux命令Netstat
1.需求 了解Netstat命令 2.简介 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multi ...
- Android 自定义动画 Loading
转自:http://my.oschina.net/janson2013/blog/118558 1.定义一个ImageView 定义一个ImageView是为了装载图片,其中的图片将被rotate用来 ...
- hihoCoder 1015KMP
#include <iostream> #include <algorithm> #include <stdio.h> #include <math.h> ...
- SQL联合查询(内联、左联、右联、全联)语法
SQL联合查询(内联.左联.右联.全联)语法 概述: 联合查询效率较高,举例子来说明联合查询:内联inner join .左联left outer join .右联right outer join ...
- 关于java1.7集合源码阅读
工作中每天都会和java集合打交道,虽然以前也看过jdk源码的实现,但有些东西时间长了还是会遗忘,或者有些实现在新版本中有了新的变化,俗话说"温故而知新",所以打算再阅读一下相关源 ...
- Fiddler抓包6-get请求(url详解)【转载】
本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/fiddler/ 前言 上一篇介绍了Composer的功能,可以模拟get和post请求 ...
- blog postman 翻译(2):postman中新增request attributes 的描述(desc)
blog的原文链接是:http://blog.getpostman.com/2017/06/03/descriptions-for-request-attributes-in-postman/ pos ...
- 华农oj 2192: hzk又在打人【CRT合并/待补】
2192: hzk又在打人 Time Limit: 12 Sec Memory Limit: 512 MB Submit: 52 Solved: 1 [Submit][Status][Web Boar ...