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