JAVA解析远程Linux日志_java远程访问linux服务器读取分析日志文件

该博客展示了如何使用JAVA通过SSH2库连接并登录远程Linux服务器,执行命令读取和分析日志文件。主要涉及了连接、认证、执行命令、处理输出流等关键步骤。
摘要由CSDN通过智能技术生成

分析linux服务器上的大量日志文件

package com.iflytek.jtcn.service.impl.demo;

import ch.ethz.ssh2.Connection;

import ch.ethz.ssh2.Session;

import ch.ethz.ssh2.StreamGobbler;

import org.apache.commons.lang3.StringUtils;

import org.elasticsearch.client.Client;

import java.io.*;

import java.util.*;

public class CallLinuxCommand {

//字符编码默认是utf-8

private static String DEFAULTCHART = "UTF-8";

private static String command1;

private Connection conn;

private String ip;

private String userName;

private String password;

public CallLinuxCommand(String ip, String userName, String password) {

this.ip = ip;

this.userName = userName;

this.password = password;

}

/**

* 远程登录linux的主机

*

* @return 登录成功返回true,否则返回false

* @author Ickes

* @since V0.1

*/

public Boolean login() {

boolean flg = false;

try {

conn = new Connection(ip);

conn.connect();//连接

flg = conn.authenticateWithPassword(userName, password);//认证

} catch (IOException e) {

e.printStackTrace();

}

return flg;

}

/**

* @param

* @return 命令执行完后返回的结果值

* @author Ickes

* 远程执行shll脚本或者命令

* @since V0.1

*/

public String execute(String cmd) {

String result = "";

try {

if (login()) {

Session session = conn.openSession();//打开一个会话

session.execCommand(cmd);//执行命令

result = processStdout(session.getStdout(), DEFAULTCHART);

System.out.println("cmd:" + cmd);

System.out.println("result:" + result);

System.out.println("------------------------------------------------");

//如果为得到标准输出为空,说明脚本执行出错了

if (StringUtils.isBlank(result)) {

result = processStdout(session.getStderr(), DEFAULTCHART);

}

conn.close();

session.close();

}

} catch (IOException e) {

e.printStackTrace();

}

return result;

}

/**

* 解析脚本执行返回的结果集

*

* @param in 输入流对象

* @param charset 编码

* @return 以纯文本的格式返回

* @author Ickes

* @since V0.1

*/

private String processStdout(InputStream in, String charset) {

InputStream stdout = new StreamGobbler(in);

StringBuffer buffer = new StringBuffer();

try {

BufferedReader br = new BufferedReader(new InputStreamReader(stdout, charset));

String line = null;

while ((line = br.readLine()) != null) {

buffer.append(line + "\n");

}

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return buffer.toString();

}

/**

* @param cmd 即将执行的命令

* @return 命令执行成功后返回的结果值,如果命令执行失败,返回空字符串,不是null

* @author Ickes

* 远程执行shll脚本或者命令

* @since V0.1

*/

public String executeSuccess(String cmd) {

String result = "";

try {

if (login()) {

Session session = conn.openSession();//打开一个会话

session.execCommand(cmd);//执行命令

result = processStdoutSuccess(session.getStdout(), DEFAULTCHART);

conn.close();

session.close();

}

} catch (IOException e) {

e.printStackTrace();

}

return result;

}

/**

* 解析脚本执行返回的结果集

*

* @param in 输入流对象

* @param charset 编码

* @return 以纯文本的格式返回

* @author Ickes

* @since V0.1

*/

private String processStdoutSuccess(InputStream in, String charset) {

InputStream stdout = new StreamGobbler(in);

StringBuffer buffer = new StringBuffer();

try {

BufferedReader br = new BufferedReader(new InputStreamReader(stdout, charset));

String line = null;

while ((line = br.readLine()) != null) {

//System.out.println("processStdoutSuccess---line:" + line);

buffer.append(line + "\n");

}

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return buffer.toString();

}

public static void setCharset(String charset) {

DEFAULTCHART = charset;

}

public Connection getConn() {

return conn;

}

public void setConn(Connection conn) {

this.conn = conn;

}

public String getIp() {

return ip;

}

public void setIp(String ip) {

this.ip = ip;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public static void main(String[] args) throws Exception {

Properties prop = new Properties();

prop.load(new InputStreamReader(Client.class.getClassLoader().getResourceAsStream("application.properties"), "UTF-8"));

String command = (String) prop.get("command");

String ip = (String) prop.get("linux.ip");

String username = (String) prop.get("linux.username");

String password = (String) prop.get("linux.password");

System.out.println("ip:" + ip + "username:" + username + "password:" + password);

CallLinuxCommand rec = new CallLinuxCommand(ip, username, password);

//执行命令

System.out.println("过车日志");

System.out.println("command:" + command);

String log1 = rec.execute(command);

String log2 = rec.executeSuccess(command);

System.out.println("log1:" + log1);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值