通过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"),否则有可能出现乱码