小白开发,想咨询如何使用java实现对服务器日志log文件进行按条件查询
说的不是很清楚,你的日志存在哪里的?数据库、还是写在 log.txt 中的
服务器上面的.log文件
想通过java实现linux指令查询指定文档的内容
// command 就是你想执行的指令
String command = "";
Process process = Runtime.getRuntime().exec(command);
// 读取结果
InputStream inputStream = process.getInputStream();
您好,我是问答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632
package com.lt.common.utils.bussinesUtils;
import java.io.IOException;
import java.io.InputStream;
import com.jcraft.jsch.*;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
public class SSHLinuxUtil {
public static void main(String[] args) throws IOException, JSchException {
String command = "ll";//linux 命令
String res = exeCommand(command);
System.out.println(res);
}
public static String exeCommand(String command) {
String host = "127.0.0.1";//你的服务器ip
int port = 22;//ssh端口
String user = "root";// 用户名
String password = "";// 密码
Session session = getSession(host, port, user, password);
ChannelExec channelExec = null;
String out = "";
try {
channelExec = (ChannelExec) session.openChannel("exec");
InputStream in = channelExec.getInputStream();
channelExec.setCommand(command);
InputStream errOut= channelExec.getErrStream();
channelExec.connect();
out = IOUtils.toString(in, "UTF-8");
if(StringUtils.isBlank(out)){
out=IOUtils.toString(errOut, "UTF-8");
}
} catch (JSchException e) {
out = e.getLocalizedMessage();
} catch (IOException e) {
} finally {
channelExec.disconnect();
session.disconnect();
}
return out;
}
private static Session getSession(String host, int port, String user, String password) {
Session session = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(user, host, port);
session.setConfig("StrictHostKeyChecking", "no");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
} catch (JSchException e) {
e.printStackTrace();
}
return session;
}
}
java 连接linux服务器 执行命令工具类