首页 > 代码库 > java调用shell脚本,并且返回结果集

java调用shell脚本,并且返回结果集

 1 /**
 2      * 运行shell脚本
 3      * @param shell 需要运行的shell脚本
 4      */
 5     public static void execShell(String shell){
 6         try {
 7             Runtime rt = Runtime.getRuntime();
 8             rt.exec(shell);
 9         } catch (Exception e) {
10             e.printStackTrace();
11         }
12     }
13  
14  
15 /**
16      * 运行shell
17      *
18      * @param shStr
19      *            需要执行的shell
20      * @return
21      * @throws IOException
22      */
23     public static List runShell(String shStr) throws Exception {
24         List<String> strList = new ArrayList();
25  
26         Process process;
27         process = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",shStr},null,null);
28         InputStreamReader ir = new InputStreamReader(process
29                 .getInputStream());
30         LineNumberReader input = new LineNumberReader(ir);
31         String line;
32         process.waitFor();
33         while ((line = input.readLine()) != null){
34             strList.add(line);
35         }
36          
37         return strList;
38     }

远程登陆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   
 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

java调用shell脚本,并且返回结果集