1. 程式人生 > 實用技巧 >java遠端訪問linux伺服器讀取分析日誌檔案

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


}