纵有疾风起
人生不言弃

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

[plain] view plaincopy在CODE上查看代码片派生到我的代码片

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

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

 

新建maven项目,添加依赖:

[html] view plaincopy在CODE上查看代码片派生到我的代码片

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

 

编写一个工具类:

[java] view plaincopy在CODE上查看代码片派生到我的代码片

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

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

test22

xiaojun

 

文章转载于:https://www.cnblogs.com/kxdblog/p/4319340.html

原著是一个有趣的人,若有侵权,请通知删除

未经允许不得转载:起风网 » java调用shell脚本,并获得结果集的例子
分享到: 生成海报

评论 抢沙发

评论前必须登录!

立即登录