標籤:stack 測試 command style 添加 span chmod std hadoop
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 * 運行shell17 *18 * @param shStr19 * 需要執行的shell20 * @return21 * @throws IOException22 */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(process29 .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指令碼,並且返回結果集