zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    ThinkPHP5 动态生成图片缩略图
    2020年python学习进阶方向
    2020年一线大厂月薪35K的Python开发要求
    swoole扩展怎么用
    如何在PHP框架里把Traits使用起来
    php与Redis实现一个100万用户的投票项目,如何实现实时查看投票情况?
    PHP高并发和大流量的解决方案
    phper使用MySQL 针对千万级的大表要怎么优化?
    swoole加密可破解吗
    轻松玩转windows之redis实战
  • 原文地址:https://www.cnblogs.com/kxdblog/p/4319340.html
Copyright © 2011-2022 走看看