/**
* 运行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

  1. #!/bin/bash
  2. echo 'test22'
  3. echo $1

$1是脚本传进来的第一个参数,我们控制台打印一下这个参数

新建maven项目,添加依赖:

  1. <dependency>
  2. <groupId>org.jvnet.hudson</groupId>
  3. <artifactId>ganymed-ssh2</artifactId>
  4. <version>build210-hudson-1</version>
  5. </dependency>

编写一个工具类:

  1. package com.xj.runshell;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.nio.charset.Charset;
  5. import ch.ethz.ssh2.Connection;
  6. import ch.ethz.ssh2.Session;
  7. public class RemoteShellTool {
  8. private Connection conn;
  9. private String ipAddr;
  10. private String charset = Charset.defaultCharset().toString();
  11. private String userName;
  12. private String password;
  13. public RemoteShellTool(String ipAddr, String userName, String password,
  14. String charset) {
  15. this.ipAddr = ipAddr;
  16. this.userName = userName;
  17. this.password = password;
  18. if (charset != null) {
  19. this.charset = charset;
  20. }
  21. }
  22. public boolean login() throws IOException {
  23. conn = new Connection(ipAddr);
  24. conn.connect(); // 连接
  25. return conn.authenticateWithPassword(userName, password); // 认证
  26. }
  27. public String exec(String cmds) {
  28. InputStream in = null;
  29. String result = "";
  30. try {
  31. if (this.login()) {
  32. Session session = conn.openSession(); // 打开一个会话
  33. session.execCommand(cmds);
  34. in = session.getStdout();
  35. result = this.processStdout(in, this.charset);
  36. session.close();
  37. conn.close();
  38. }
  39. } catch (IOException e1) {
  40. e1.printStackTrace();
  41. }
  42. return result;
  43. }
  44. public String processStdout(InputStream in, String charset) {
  45. byte[] buf = new byte[1024];
  46. StringBuffer sb = new StringBuffer();
  47. try {
  48. while (in.read(buf) != -1) {
  49. sb.append(new String(buf, charset));
  50. }
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54. return sb.toString();
  55. }
  56. /**
  57. * @param args
  58. */
  59. public static void main(String[] args) {
  60. RemoteShellTool tool = new RemoteShellTool("192.168.27.41", "hadoop",
  61. "hadoop", "utf-8");
  62. String result = tool.exec("./test.sh xiaojun");
  63. System.out.print(result);
  64. }
  65. }

main函数中执行了./test.sh xiaojun这个命令,控制台打印出:

test22

xiaojun

java调用shell脚本,并获得结果集的例子的更多相关文章

  1. Java 调用 shell 脚本详解

    这一年的项目中,有大量的场景需要Java 进程调用 Linux的bash shell 脚本实现相关功能. 从之前的项目中拷贝的相关模块和网上的例子来看,有个别的“陷阱”造成调用shell 脚本在某些特 ...

  2. java调用shell脚本小demo

    复制指定文件cpp.sh: [root@localhost soft]# vim cpp.sh#!/bin/bash name="$1"\cp /home/soft/test/${ ...

  3. [转载]JAVA调用Shell脚本

    FROM:http://blog.csdn.net/jj12345jj198999/article/details/11891701 在实际项目中,JAVA有时候需要调用C写出来的东西,除了JNI以外 ...

  4. java调用shell脚本

    /** * 运行shell脚本 * @param shell 需要运行的shell脚本 */ public static void execShell(String shell){ try { Run ...

  5. JAVA调用shell脚本利用ansible修改多节点上的redis参数

    创建hosts文件 创建ansible-playbook执行时所用到的hosts文件,例如 /etc/redis/hosts 利用shell命令根据传入的host名和地址写入hosts文件: #set ...

  6. java调用shell脚本执行操作

    //定时清空 日志 String shellString = "sh /home/jyapp/delete_log.sh"; Process process = Runtime.g ...

  7. Java 执行Shell脚本指令

    一.介绍 有时候我们在Linux中运行Java程序时,需要调用一些Shell命令和脚本.而Runtime.getRuntime().exec()方法给我们提供了这个功能,而且Runtime.getRu ...

  8. 用java代码调用shell脚本执行sqoop将hive表中数据导出到mysql

    1:创建shell脚本 touch sqoop_options.sh chmod 777 sqoop_options.sh 编辑文件  特地将执行map的个数设置为变量  测试 可以java代码传参数 ...

  9. java基础/java调用shell命令和脚本

    一.项目需求: 从某一机构获取证书,证书机构提供小工具,执行.sh脚本即可启动服务,本地调用该服务即可获取证书. 问题:linux服务器启动该服务,不能关闭.一旦关闭,服务即停止. 解决方案:java ...

随机推荐

  1. python-networkx学习(1)

    介绍: networkx是python的一个库,它为图的数据结构提供算法.生成器以及画图工具.近日在使用ryu进行最短路径获取,可以通过该库来简化工作量.该库采用函数方式进行调用相应的api,其参数类 ...

  2. Docker for Web Developers目录

    在OpenStack在私有云占主导定位之后,后起之秀Docker在PaaS平台.CI/CD.微服务领域展露锋芒.作为Web Developers,我们有必要学习和掌握这门技术. 1. 运行第一个Doc ...

  3. Docker最佳实践-部署LNMP环境

    标签(linux): docker 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 环境准备 [root@docker ~]# cat /etc/redhat-r ...

  4. 简易发号SQL,可用于生成指定前缀自增序列,如订单号,生成优惠券码等

    需求1:订单号要求唯一.长度不太长.自增.但不能通过早上订单号和晚上订单号相减推算出平台大概一天的单量 需求2:要求生成10w张优惠券,要求券码唯一.不能太长,不能轻易猜测出其他券码 根据这些需求提供 ...

  5. 解决mysql、vsftp远程连接速度慢的问题

    以 centOS 6.3(其他操作系统类似,同样适用)说明: 当我们的服务都配置正常的情况下,有时会出现连接速度慢而导致连接失败的问题 问题分析:这些情况一般都是DNS解析惹的祸 mysql连接速度慢 ...

  6. 在ubtunu使用apt-get安装和配置freeradius

    安装freeradius之前先要确保已经安装了mysql.因为后来要用到.没有安装使用apt-get来安装 apt-get install mysql-server 然后就是安装freeradius的 ...

  7. Ubuntu14.04 命令行下安装teamviewer

    下载teamviewer 链接:https://pan.baidu.com/s/1hs0BppM  密码:sdmk 上传到 /home/[user] cd /home/[user] 移动安装包到 /o ...

  8. 让44.1版本的sketch打开更高版本的sketch文件

    我们都知道,sketch的有效license与版本挂钩.最近设计师又更新了sketch版本,导致她生成的源文件我都无法打开. 毕竟我不是使用sketch进行UI设计,仅用它来查看设计稿参数,再花99美 ...

  9. Spring整合quartz框架实现任务定时调度

    1.  首先需要引入需要的jar包,如上图所示. 2. 编写需要定时调度的测试类: package com.jp.task; import java.util.Date; public class T ...

  10. Apache优化配置:修改最大并发连接数

    此文摘自: http://www.365mini.com/page/apache-concurrency-configuration.htm Apache查看连接数和限制当前的连接数 : http:/ ...