Java远程实现Linux文件内容读取(通过远程执行shell命令分析日志)
Posted 零度anngle
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java远程实现Linux文件内容读取(通过远程执行shell命令分析日志)相关的知识,希望对你有一定的参考价值。
使用的是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);
以上是关于Java远程实现Linux文件内容读取(通过远程执行shell命令分析日志)的主要内容,如果未能解决你的问题,请参考以下文章
Java远程实现Linux文件内容读取(通过远程执行shell命令分析日志)
Java远程实现Linux文件内容读取(通过远程执行shell命令分析日志)
[java]如何在windows下读取远程的linux下面的文件?
Ubuntu下通过Firefox Opera Chromium浏览器直接执行java应用程序(打开java jnlp文件)实现在服务器远程虚拟控制台完成远程管理的方法