java 远程 shell脚本_Java 远程调用 shell脚本
* pom.xml 引入相关jar包
```
ch.ethz.ganymed
ganymed-ssh2
262
```
* 远程服务器上代码
```
#/bin/bash
echo "hello world"
```
* 相关Java代码
```
public class RemoteShellExecutor {
private Connection conn;
private String ip;
private String osUserName;
private String password;
private String charset = Charset.defaultCharset().toString();
private static final int TIME_OUT = 1000 * 5 * 60;
public RemoteShellExecutor(String ip, String username, String password) {
this.ip = ip;
this.osUserName = username;
this.password = password;
}
private boolean login() throws IOException {
conn = new Connection(ip);
conn.connect();
return conn.authenticateWithPassword(osUserName, password);
}
public int exec(String cmds) throws IOException {
InputStream stdOut = null;
InputStream stdErr = null;
String outStr = "";
String outErr = "";
int ret = 1;
if (login()) {
try {
Session session = conn.openSession();
session.execCommand(cmds);
stdOut = new StreamGobbler(session.getStdout());
outStr = processStream(stdOut, charset);
stdErr = new StreamGobbler(session.getStderr());
outErr = processStream(stdErr, charset);
session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
System.out.println("outStr = " + outStr);
System.out.println("outErr = " + outErr);
ret = session.getExitStatus();
} finally {
if (conn != null) {
conn.close();
}
IOUtils.closeQuietly(stdOut);
IOUtils.closeQuietly(stdErr);
}
}
return ret;
}
public String processStream(InputStream in, String charset)
throws UnsupportedEncodingException, 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) throws IOException {
RemoteShellExecutor rse = new RemoteShellExecutor("IP", "root", "12312");
System.out.println(rse.exec("/root/test_shell.sh"));
}
}
```
* 输出结果
```
outStr = hello world
outErr =
0
```
java 远程 shell脚本_Java 远程调用 shell脚本相关推荐
- java 执行js脚本_java中调用js脚本
JDK1.6加入了对Script(JSR223)的支持.这是一个脚本框架,提供了让脚本语言来访问Java内部的方法.你可以在运行的时候找到脚本引擎,然后调用这个引擎去执行脚本.这个脚本API允许你为脚 ...
- java channelexec_java-使用SSH exec通道调用Shell脚本,但忽略对其他Shell脚本的调用
我正在使用此处提供的有用示例,使用JSch exec在远程服务器上执行Shell脚本.我可以看到脚本返回的回显,并且结尾处的退出状态为0-乍一看,一切看起来都很不错. 但是,问题在于脚本本身会调出其他 ...
- C语言程序中调用脚本,C语言调用SHELL脚本
在Linux 环境下Shell脚本具有非常强大的功能!使用Shell可以很方便的使用和管理Linux系统,最近学习了一点shell知识,所以一直在 想要是可以在C/C++中调用shell脚本那该有多 ...
- java 调用js脚本_Java中使用JavaScript脚本的方法步骤
简介 Nashorn Nashorn 一个 javascript 引擎. 从JDK 1.8开始,Nashorn取代Rhino(JDK 1.6, JDK1.7)成为Java的嵌入式JavaScript引 ...
- java文件调用脚本_Java中调用文件中所有bat脚本
//调用外部脚本 String fileips=null;//所有的路径 String[] files=null; String fileip=null;//单个路径 try { InputStrea ...
- python调用shell命令-用Python调用Shell命令
Python经常被称作"胶水语言",因为它能够轻易地操作其他程序,轻易地包装使用其他语言编写的库,也当然可以用Python调用Shell命令. 用Python调用Shell命令有如 ...
- java lua脚本_Java中使用LUA脚本语言
Lua 是一个小巧的脚本语言.是巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de Janeiro)里的一个研究小组,由Roberto Ieru ...
- java 执行cmd 堵塞_java中调用cmd命令被阻塞无法返回和继续执行
在项目中用到了数据库的备份和恢复功能,就通过java的Runtime.getRuntime().exec()来调用cmd或者linux命令来导出和导入.sql文件.但是在执行恢复时,程序一直被阻塞,没 ...
- 【Groovy】Groovy 脚本调用 ( Linux 中调用 Groovy 脚本 | Windows 中调用 Groovy 脚本 )
文章目录 前言 一.Linux 中调用 Groovy 脚本 二.Windows 中调用 Groovy 脚本 前言 在 命令行 , Groovy 脚本 , Groovy 类 , Java 类中 , 可以 ...
最新文章
- MS SQL SERVER 2005 用于Web开发的安装注意事项
- 【机器视觉】 fuzzy_measure_pos算子
- RxSwift 系列(一)
- java中大数字表示什么_JAVA中大数字的的处理:BigInteger和BigDecimal
- [C++] - 单例模式 释放资源 析构函数的应用
- 数据:以太坊上借贷协议资金规模年内增长幅度超300%
- python打印文件到字符串_请问Python打印字符串到文本文件
- DSAPI QQ用户相关
- echarts 获取点击的y轴数值_ECharts关系图
- $.ajax跳入error之 async
- 如何编写一份优质的可行性研究报告
- SumatraPDF 适配 EPUB,MOBI
- RF射频技术-si4438C芯片介绍
- NPOI SetRowBreak 插入分页符无用
- DWcs4右下角没有html文件,dreamweaver cs4 (dwcs4)安装图文教程
- visual studio 中鼠标光标的切换(或鼠标光标变成阴影方块)
- 【分享一个好看的Android item布局 使用于ListView,RecyleView JAVA版本(二)】
- 这篇 ElasticSearch 详细使用教程,内部分享时被老大表扬了
- CSS的世界(十四)
- C++ Reference: Standard C++ Library reference: C Library: cstdio: FILE