java执行linux shell命令,并拿到返回值
1 package com.pasier.xxx.util; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.nio.charset.Charset; 6 7 import org.slf4j.Logger; 8 import org.slf4j.LoggerFactory; 9 10 import ch.ethz.ssh2.ChannelCondition; 11 import ch.ethz.ssh2.Connection; 12 import ch.ethz.ssh2.Session; 13 import ch.ethz.ssh2.StreamGobbler; 14 15 public class RmtShellExecutor { 16 17 private static final Logger LOG = LoggerFactory.getLogger(RmtShellExecutor.class); 18 19 private Connection conn; 20 private String ip; 21 private String usr; 22 private String psword; 23 private String charset = Charset.defaultCharset().toString(); 24 25 private static final int TIME_OUT = 1000 * 5 * 60; 26 27 public RmtShellExecutor(String ip, String usr, String ps) { 28 this.ip = ip; 29 this.usr = usr; 30 this.psword = ps; 31 } 32 33 private boolean login() throws IOException { 34 conn = new Connection(ip); 35 conn.connect(); 36 return conn.authenticateWithPassword(usr, psword); 37 } 38 39 public String exec(String cmds) throws IOException { 40 InputStream stdOut = null; 41 InputStream stdErr = null; 42 String outStr = ""; 43 String outErr = ""; 44 int ret = -1; 45 46 try { 47 if (login()) { 48 Session session = conn.openSession(); 49 session.execCommand(cmds); 50 stdOut = new StreamGobbler(session.getStdout()); 51 outStr = processStream(stdOut, charset); 52 LOG.info("caijl:[INFO] outStr=" + outStr); 53 stdErr = new StreamGobbler(session.getStderr()); 54 outErr = processStream(stdErr, charset); 55 LOG.info("caijl:[INFO] outErr=" + outErr); 56 session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT); 57 ret = session.getExitStatus(); 58 59 } else { 60 LOG.error("caijl:[INFO] ssh2 login failure:" + ip); 61 throw new IOException("SSH2_ERR"); 62 } 63 64 } finally { 65 if (conn != null) { 66 conn.close(); 67 } 68 if (stdOut != null) 69 stdOut.close(); 70 if (stdErr != null) 71 stdErr.close(); 72 } 73 74 return outStr; 75 } 76 77 private String processStream(InputStream in, String charset) throws IOException { 78 byte[] buf = new byte[1024]; 79 StringBuilder sb = new StringBuilder(); 80 while (in.read(buf) != -1) { 81 sb.append(new String(buf, charset)); 82 } 83 return sb.toString(); 84 } 85 86 public static void main(String[] args) { 87 88 String usr = "root"; 89 String password = "12345"; 90 String serverIP = "11.22.33.xx"; 91 String shPath = "/root/ab.sh"; 92 93 RmtShellExecutor exe = new RmtShellExecutor(serverIP, usr, password); 94 95 String outInf; 96 97 try { 98 outInf = exe.exec("sh " + shPath + " xn"); 99 System.out.println("outInf= " + outInf); 100 } catch (IOException e) { 101 e.printStackTrace(); 102 } 103 } 104 105 }
java执行linux shell命令,并拿到返回值相关推荐
- java执行Linux shell命令或者Window cmd命令
执行Linux或者Window下的命令的代码: public static void runbat(String batFilePath) { if(System.getProperty( ...
- python commands用法_python之commands模块(执行Linux Shell命令)
commands模块 用于执行Linux shell命令,要获得shell命令的输出只需要在后面参数写入('命令')就可以了. 需要得到命令执行的状态则需要判断$?的值, 在Python中有一个模块c ...
- java 利用ganymed-ssh2 远程执行linux shell 命令
早期的ganymed-ssh2包也可以实现远程调研linux shell 命令,但是这个包最新版本是2014年之后,就没有更新了,发现linux 操作系统安装 open-ssh 8.5及更高级版本,就 ...
- python commands执行不连续_[Python] 利用commands模块执行Linux shell命令
用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态和结果,下面是commands模块的3个主要 ...
- linux 变量函数返回值,linux shell 自定义函数(定义、返回值、变量作用域)介绍...
linux shell 可以用户定义函数,然后在shell脚本中可以随便调用.下面说说它的定义方法,以及调用需要注意那些事项. 一.定义shell函数(define function) 语法: [ f ...
- linux shell 自定义函数(定义、返回值、变量作用域)介绍
linux shell 可以用户定义函数,然后在shell脚本中可以随便调用.下面说说它的定义方法,以及调用需要注意那些事项. 一.定义shell函数(define function) 语法: [ f ...
- linux无filelength函数,Linux Shell 自定义函数(定义、返回值、变量作用域)介绍
定义Shell函数(define function) 语法: [ function ] funname [()] { action; [return int;] } 说明: 可以带function f ...
- java 子线程传参_踩坑之Java执行Linux命令死锁阻塞挂起
1 问题背景 最近在做一个需求需要调用linux下的某个脚本来对ai的模型进行训练,很简单的需求,我像往常一样写下如下的代码片段: Process process = Runtime.getRunti ...
- php java执行linux_java_java执行Linux命令的方法,本文实例讲述了java执行Linux命 - phpStudy...
java执行Linux命令的方法 本文实例讲述了java执行Linux命令的方法.分享给大家供大家参考.具体实现方法如下: public class StreamGobbler extends Thr ...
最新文章
- linux-glibc内存管理小结2(内存相关系统调用的实现)
- 套接字编程——基于UDP协议
- POJ 1041 John's trip(欧拉回路)
- shell编程中特殊字符的问题总结
- python 共轭转置_python矩阵运算,转置,逆运算,共轭矩阵实例
- sap系统搭建教程_詹迟迟:如何搭建知识付费系统?知识付费系统搭建教程
- java异常处理简介_【自学java】异常处理简介
- 欧菲光修正2020年业绩为预计亏损18.5亿元
- es6中class类的全方面理解
- 每天一个linux命令(6) ar命令
- 智慧环卫管理系统解决方案
- 均值、方差、标准差、协方差详解及MATLAB实现
- 幻想世界正在连接服务器,旅行物语桌面版连接不上怎么办 pc电脑版无法连接服务器怎么办...
- esayExcel自定义注解导出表头批注
- 【逗老师带你学IT】通过企业微信推送AD域密码即将到期提醒
- 2017中国云计算评测报告 1
- C#使用ffmpeg抽帧压缩mp4
- vue实现 修改密码
- 游戏中的数学与物理(一)
- Sparse Matrix, MUMPS
热门文章
- matlab 图像显著性检测ft_图像显著性检测总结
- zabbix编译php_【Zabbix学习笔记】五、LNMP之PHP7.4.1源码编译安装
- Spring Boot Vue从零开始搭建系统(三):项目前后端分离
- golang 接口_「实战」助力数据库开发之接口篇 - Golang 连接 Greenplum
- mysql安装start service错误_为什么安装mysql会出现start service错误
- mysql 事物 锁行 测试_MySQL Transaction--RC事务隔离级别下加锁测试
- idea远程调试修改代码_使用IDEA远程调试线上代码
- mysql install语句_mysql8 参考手册--INSTALL COMPONENT语句
- mysql 执行计划大于_Mysql执行计划(大章)
- Node.js 15 正式发布,14 将支持到 2023 年