通過maven庫獲取ganymed-ssh2-262.jar,這是一個實現了ssh2協議的工具包,可以遠程連接linux機器,執行命令,有些工作全靠它了
示例代碼如下:
<!--首先要建立連接,傳入ip(默認端口22),登錄用戶名和密碼-->
private static Connection getConnection(String hostname, String username, String password) throws Exception { Connection conn = null; try { conn = new Connection(hostname); conn.connect(); boolean isAuthenticated = conn.authenticateWithPassword(username, password); if (isAuthenticated == false) { throw new IOException("Authentication failed."); } } catch (Exception e) { throw new IOException("username or password error."); } return conn; }
<!--執行一條命令,傳入connect相關參數,命令和超時時間-->
public static String execRemoteCommand(String hostname, String username, String password, String command, long timeout) throws Exception { Connection conn = getConnection(hostname, username, password); StringBuilder sb = new StringBuilder(); Session session = null; try { session = conn.openSession(); session.requestPTY("vt100", 80, 24, 640, 480, null); session.execCommand(command); InputStream stdout = new StreamGobbler(session.getStdout()); BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); long start = System.currentTimeMillis(); char[] arr = new char[512]; int read; int i = 0; while (true) { read = br.read(arr, 0, arr.length); if (read < 0 || (System.currentTimeMillis() - start) > timeout * 1000) { break; } sb.append(new String(arr, 0, read)); i++; } } finally { if (session != null) { session.close(); } if (conn != null) { conn.close(); } } return sb.toString(); }
<!--執行多條命令,傳入connect相關參數,命令和超時時間-->
public static String execRemoteCommand(String hostname, String username, String password, String[] command, long timeout) throws Exception { Connection conn = getConnection(hostname, username, password); StringBuilder sb = new StringBuilder(); Session session = null; try { for (int t = 0; t < command.length; t++) { session = conn.openSession(); session.requestPTY("vt100", 80, 24, 640, 480, null); session.execCommand(command[t]); InputStream stdout = new StreamGobbler(session.getStdout()); BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); long start = System.currentTimeMillis(); char[] arr = new char[512]; int read; int i = 0; while (true) { read = br.read(arr, 0, arr.length); if (read < 0 || (System.currentTimeMillis() - start) > timeout * 1000) { break; } sb.append(new String(arr, 0, read)); i++; } session.close(); } } finally { if (conn != null) { conn.close(); } } return sb.toString(); }
最近用這個工具包做了個遠程下載的功能
OutputStream out = response.getOutputStream(); Connection conn = getConnection(sshcfg.getHost(), sshcfg.getUsername(), sshcfg.getPassword()); SCPInputStream ins=null; try { SCPClient scpClient = conn.createSCPClient(); ins = scpClient.get(fpath); //InputStream stdout = new StreamGobbler(ins); byte[] arr = new byte[512]; int read; while (true) { read = ins.read(arr); if (read < 0) break; out.write(arr); } //ins.close(); } finally { if(ins!=null)ins.close(); if (conn != null) { conn.close(); } }
注意:new InputStreamReader(stdout)這個的使用的是默認的字符編碼,如果執行”cat **.log“而文件中有中文,應當指定編碼,比如:new InputStreamReader(stdout,"GBK"),否則有可能出現亂碼