Java远程实现Linux文件内容读取(通过远程执行shell命令分析日志)

16 篇文章 0 订阅

   使用的是com.jcraft.jsch包工具,google进行下载,代码比较简单分析用户站内搜索日志,分析用户搜索关键词,代码如下:

package com.cloud.hotword.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.cloud.hotword.domain.vo.WordFrequency;
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  2020/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();
	    }
	}
	
	
	/**
	 * 
	 * @param logPath
	 * @param fileList
	 * @param user
	 * @param passwd
	 * @param host
	 * @param port
	 * @return
	 * @throws Exception
	 */
	public static Map<String, WordFrequency> execCmdAndgetVFAndUF(Map<String, WordFrequency> map, String logPath, List<File> fileList, String user, String passwd, String host, int port) throws Exception {

		Session session= connect(user, passwd,host,port);	  
	    try {
			for (File f : fileList) {
				String command = "cd " + logPath + "; cat " +logPath+"/"+ f.getName();
				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(map, buf);
					}
				} finally {
					reader.close();
					channel.disconnect();
				}

			} 
		} finally {
			session.disconnect();
		}
	    return map;
	}
	
	
	/**
     * 解析一行日志,存放到map中并返回
     * @param map
     * @param str
     * @return
     */
	private static Map<String, WordFrequency> analyseLogOneLine(Map<String, WordFrequency> map, String str) {
		String[] logLine  = str.split("\\s+\\|\\s+");
		String userInput = null;
		String ip=null;
		if(logLine!=null && logLine.length==5){
			ip= logLine[1].trim();
			userInput = StringUtils.substring(logLine[4], 1, logLine[4].length()-1);
		}		        
		if(StringUtils.isNotEmpty(userInput)){	        	
			String[]  keys =   userInput.split("\\s+");
			for(String key : keys){		    					    			
				if(map.containsKey(key)){
					map.get(key).setFrequency(map.get(key).getFrequency()+1);
					map.get(key).getIpSet().add(ip);
				}else{
					Set<String> ipSet = new HashSet<String>();
					ipSet.add(ip);
					WordFrequency wf = new WordFrequency(1L,ipSet);
					map.put(key,wf);
				}
			}		        	
		}
		return map;
	}
	
	
	public static void main(String[] args) throws Exception {
		
		execCmd("cd /web/logs/; cat search-api.log-2020-06-07.0.log","yunpub","xxxxxx","112.xx.xx.xx",22);
	}

}



 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值