java远程访问linux服务器读取分析日志文件

分析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);
    }


}

  

posted on 2020-09-02 17:25  shenhaha  阅读(2198)  评论(0编辑  收藏  举报

有用的话麻烦点个推荐哈