java调用shell脚本,并获得结果集的例子
/**
* 运行shell脚本
* @param shell 需要运行的shell脚本
*/
public static void execShell(String shell){
try {
Runtime rt = Runtime.getRuntime();
rt.exec(shell);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 运行shell
*
* @param shStr
* 需要执行的shell
* @return
* @throws IOException
*/
public static List runShell(String shStr) throws Exception {
List<String> strList = new ArrayList(); Process process;
process = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",shStr},null,null);
InputStreamReader ir = new InputStreamReader(process
.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
process.waitFor();
while ((line = input.readLine()) != null){
strList.add(line);
} return strList;
}
远程登陆linux且调用shell
首先在远程服务器上编写一个测试脚本test.sh,并赋予可执行权限:chmod +x test.sh
- #!/bin/bash
- echo 'test22'
- echo $1
$1是脚本传进来的第一个参数,我们控制台打印一下这个参数
新建maven项目,添加依赖:
- <dependency>
- <groupId>org.jvnet.hudson</groupId>
- <artifactId>ganymed-ssh2</artifactId>
- <version>build210-hudson-1</version>
- </dependency>
编写一个工具类:
- package com.xj.runshell;
- import java.io.IOException;
- import java.io.InputStream;
- import java.nio.charset.Charset;
- import ch.ethz.ssh2.Connection;
- import ch.ethz.ssh2.Session;
- public class RemoteShellTool {
- private Connection conn;
- private String ipAddr;
- private String charset = Charset.defaultCharset().toString();
- private String userName;
- private String password;
- public RemoteShellTool(String ipAddr, String userName, String password,
- String charset) {
- this.ipAddr = ipAddr;
- this.userName = userName;
- this.password = password;
- if (charset != null) {
- this.charset = charset;
- }
- }
- public boolean login() throws IOException {
- conn = new Connection(ipAddr);
- conn.connect(); // 连接
- return conn.authenticateWithPassword(userName, password); // 认证
- }
- public String exec(String cmds) {
- InputStream in = null;
- String result = "";
- try {
- if (this.login()) {
- Session session = conn.openSession(); // 打开一个会话
- session.execCommand(cmds);
- in = session.getStdout();
- result = this.processStdout(in, this.charset);
- session.close();
- conn.close();
- }
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- return result;
- }
- public String processStdout(InputStream in, String charset) {
- byte[] buf = new byte[1024];
- StringBuffer sb = new StringBuffer();
- try {
- while (in.read(buf) != -1) {
- sb.append(new String(buf, charset));
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return sb.toString();
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- RemoteShellTool tool = new RemoteShellTool("192.168.27.41", "hadoop",
- "hadoop", "utf-8");
- String result = tool.exec("./test.sh xiaojun");
- System.out.print(result);
- }
- }
main函数中执行了./test.sh xiaojun这个命令,控制台打印出:
test22
xiaojun
java调用shell脚本,并获得结果集的例子的更多相关文章
- Java 调用 shell 脚本详解
这一年的项目中,有大量的场景需要Java 进程调用 Linux的bash shell 脚本实现相关功能. 从之前的项目中拷贝的相关模块和网上的例子来看,有个别的“陷阱”造成调用shell 脚本在某些特 ...
- java调用shell脚本小demo
复制指定文件cpp.sh: [root@localhost soft]# vim cpp.sh#!/bin/bash name="$1"\cp /home/soft/test/${ ...
- [转载]JAVA调用Shell脚本
FROM:http://blog.csdn.net/jj12345jj198999/article/details/11891701 在实际项目中,JAVA有时候需要调用C写出来的东西,除了JNI以外 ...
- java调用shell脚本
/** * 运行shell脚本 * @param shell 需要运行的shell脚本 */ public static void execShell(String shell){ try { Run ...
- JAVA调用shell脚本利用ansible修改多节点上的redis参数
创建hosts文件 创建ansible-playbook执行时所用到的hosts文件,例如 /etc/redis/hosts 利用shell命令根据传入的host名和地址写入hosts文件: #set ...
- java调用shell脚本执行操作
//定时清空 日志 String shellString = "sh /home/jyapp/delete_log.sh"; Process process = Runtime.g ...
- Java 执行Shell脚本指令
一.介绍 有时候我们在Linux中运行Java程序时,需要调用一些Shell命令和脚本.而Runtime.getRuntime().exec()方法给我们提供了这个功能,而且Runtime.getRu ...
- 用java代码调用shell脚本执行sqoop将hive表中数据导出到mysql
1:创建shell脚本 touch sqoop_options.sh chmod 777 sqoop_options.sh 编辑文件 特地将执行map的个数设置为变量 测试 可以java代码传参数 ...
- java基础/java调用shell命令和脚本
一.项目需求: 从某一机构获取证书,证书机构提供小工具,执行.sh脚本即可启动服务,本地调用该服务即可获取证书. 问题:linux服务器启动该服务,不能关闭.一旦关闭,服务即停止. 解决方案:java ...
随机推荐
- PHP微信H5支付开发
近来公司项目要求用到微信H5开发,因为微信开发文档处处都是坑,我也走了不少弯路,现在就把H5支付的过程记录一下,已备后用!! 首先 先去商户平台申请开通 H5支付!!!! 我们从微信官方下载H5支付d ...
- NIO笔记---上
小弟前端时间由于开发个管理系统导致断更了近20天!!马上就要春招了,学习了一下NIO,将笔记记录下,希望和我一样的18届毕业生都能找到满意的公司!! 本文记录了NIO与IO的区别,缓冲区的数据存取,直 ...
- H3c交换机配置端口镜像详情
端口镜像 需要将G0/0/1口的全部流量镜像到G0/0/2口,即G0/0/1为源端口,G0/0/2为目的端口. 配置步骤 1.进入配置模式:system-view: 2.创建本地镜像组:mirrori ...
- [codility] Lession1 - Iterations - BinaryGap
Task1: A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is ...
- 简单的.editconfig文件
root = true [*] charset = utf-8 indent_style = space indent_size = 2 end_of_line = lf insert_final_n ...
- CSS中的字体属性和文本属性
1.CSS字体的属性 font 简写,作用是把所有的针对字体的属性设置在一个声明中 font-family 设置字体系列 font-size 设置字体尺寸 font-style 设置字体风格,ital ...
- c#写的热键注册程序
玩网页游戏挂机,又不想让别人看见你电脑的运行.所以写了个后台运行的程序. 先谁HotKey注册类,用来调用com组件来显示与隐藏窗体: using System; using System.Colle ...
- JQuery Ajax 设置请求头信息application/json
今天有个api后台接application/json格式的 在Jquery里$.ajax默认是contentType: application/x-www-form-urlencoded; chars ...
- Jedis/JedisPool和Redis数据类型与特性
1.介绍Jedis Jedis 是 Redis 的 java 版本客户端,使用Jedis可以连接 Redis的数据库,Jedis连接方式有三种Jedis/JedisPool 连接.ShardedJed ...
- 机器学习00:如何通过Python入门机器学习
我们都知道机器学习是一门综合性极强的研究课题,对数学知识要求很高.因此,对于非学术研究专业的程序员,如果希望能入门机器学习,最好的方向还是从实践触发. 我了解到Python的生态对入门机器学习很有帮助 ...