码迷,mamicode.com
首页 > 编程语言 > 详细

java调用shell脚本,并获得结果集的例子

时间:2015-03-06 23:24:47      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

/**
	 * 运行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技术分享技术分享
 
  1. #!/bin/bash  
  2. echo ‘test22‘  
  3. echo $1  


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

 

新建maven项目,添加依赖:

[html] view plaincopy技术分享技术分享
 
  1. <dependency>  
  2.     <groupId>org.jvnet.hudson</groupId>  
  3.     <artifactId>ganymed-ssh2</artifactId>  
  4.     <version>build210-hudson-1</version>  
  5. </dependency>  


 

编写一个工具类:

[java] view plaincopy技术分享技术分享
 
  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脚本,并获得结果集的例子

标签:

原文地址:http://www.cnblogs.com/kxdblog/p/4319340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!