JSch 是SSH2的一個純Java實現。它容許你鏈接到一個sshd 服務器,使用端口轉發,X11轉發,文件傳輸等等。你能夠將它的功能集成到你本身的 程序中。同時該項目也提供一個J2ME版本用來在手機上直連SSHD服務器。實現一個java工具類。html
引用:java
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
</dependency>
web
java工具類:shell
package com.citipub_zxsy.count.utils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Set; import org.apache.commons.lang3.StringUtils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelExec; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; /** * @decription 執行遠程shell命令,並獲取結果--實現分析日誌 * @author zhangwenchao * @date 2018/6/8 * */ public class ShellUtils { /**配置鏈接 * @param user * @param passwd * @param host * @param post * @throws Exception */ public static Session connect(String user, String passwd, String host,int post) throws Exception { JSch jsch = new JSch(); Session session = jsch.getSession(user, host, post); if (session == null) { throw new Exception("session is null"); } session.setPassword(passwd); java.util.Properties config = new java.util.Properties(); //第一次登錄 config.put("StrictHostKeyChecking", "no"); session.setConfig(config); try { session.connect(30000); } catch (Exception e) { throw new Exception("鏈接遠程端口無效或用戶名密碼錯誤"); } return session; } /** * @description 執行shell命令 * @param command shell 命令 * @param user 用戶名 * @param passwd 密碼 * @param host ip地址 * @param post 端口號 * @throws Exception */ public static void execCmd(String command, String user, String passwd, String host, int port) throws Exception { System.out.println(command); Session session= connect(user, passwd,host,port); BufferedReader reader = null; Channel channel = null; try { channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command); channel.setInputStream(null); ((ChannelExec) channel).setErrStream(System.err); channel.connect(); InputStream in = channel.getInputStream(); reader = new BufferedReader(new InputStreamReader(in)); String buf = null; //返回數據 while ((buf = reader.readLine()) != null) { System.out.println(buf); } } catch (IOException e) { e.printStackTrace(); } catch (JSchException e) { e.printStackTrace(); } finally { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } channel.disconnect(); session.disconnect(); } } public static List<String> execCmdAndOutput(String command, String user, String passwd, String host, int port) throws Exception { Session session= connect(user, passwd,host,port); BufferedReader reader = null; Channel channel = null; try { channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command); channel.setInputStream(null); ((ChannelExec) channel).setErrStream(System.err); channel.connect(); InputStream in = channel.getInputStream(); reader = new BufferedReader(new InputStreamReader(in)); List<String> output = new ArrayList<String>(); String buf = null; //返回數據 while ((buf = reader.readLine()) != null) { output.add(buf); } return output; } catch (IOException e) { e.printStackTrace(); } catch (JSchException e) { e.printStackTrace(); } finally { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } channel.disconnect(); session.disconnect(); } return null; } /** * * @param logPath * @param fileList * @param user * @param passwd * @param host * @param port * @return * @throws Exception */ public static Set<String> execCmdAndgetOlderUserIDs(Set<String> userIDSet, String logPath, List<String> fileNames, String user, String passwd, String host, int port) throws Exception { Session session= connect(user, passwd,host,port); try { for (String fileName : fileNames) { String command = "cat "+fileName+"|grep '/login'"; Channel channel = null; BufferedReader reader = null; try { channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command); channel.setInputStream(null); ((ChannelExec) channel).setErrStream(System.err); channel.connect(); InputStream in = channel.getInputStream(); reader = new BufferedReader(new InputStreamReader(in,"UTF-8")); String buf = null; //返回數據 while ((buf = reader.readLine()) != null) { analyseLogOneLine(userIDSet, buf); } } finally { reader.close(); channel.disconnect(); } } } finally { session.disconnect(); } return userIDSet; } /** * 解析一行日誌,獲取老用戶uid存放到Set中並返回 * @param map * @param str * @return */ private static Set<String> analyseLogOneLine(Set<String> userIDSet, String str) { String[] logLine = str.split("\\s+\\|\\s+"); String userToken = null; String cacheResult=null; if(logLine!=null && logLine.length==12){ userToken= logLine[6].trim(); cacheResult = logLine[10].trim(); } if(StringUtils.isNotEmpty(cacheResult)){ JSONObject jsonObject = JSON.parseObject(cacheResult); JSONObject body = (JSONObject)jsonObject.get("body"); if(body!=null){ String userID = (String)body.get("userID"); if(StringUtils.isNotEmpty(userID)&&StringUtils.isNotEmpty(userToken)&&userToken.contains("anony")){ userToken = userToken.substring(0, userToken.length()-6); if(!userToken.equals(userID)){ userIDSet.add(userID); } } } } return userIDSet; } public static void main(String[] args) throws Exception { execCmd("cd /web/logs/sc_zxsy; cat api-2018-06-07.0.log","yunpub","XXXXXXX","xxx.xxx.xx.xxx",22); } }