Web端查看Liunx日志,网页端查看Liunx日志
1, 导入依赖包
<dependency>
<groupId>com.hierynomus</groupId>
<artifactId>sshj</artifactId>
<version>0.29.0</version>
</dependency>
2,控制器层
package com.view.controller;
import cn.hutool.core.util.StrUtil;
import com.view.annotation.BlogAuthentication;
import com.view.result.BlogResult;
import com.view.service.BlogConfigurationService;
import com.view.util.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@RestController
@RequestMapping("/log")
public class LogController {
@Autowired
BlogConfigurationService blogConfigurationService;
@GetMapping("/list")
@ApiOperation(value = "日志",notes = "根据pageNo(行数) key(关键字) 查询日志")
public BlogResult logList(@RequestParam Integer pageNo, @RequestParam String key){
try {
return BlogResult.success(LogUtil.tail(pageNo,key,blogConfigurationService.list().get(0)));
} catch (IOException e) {
e.printStackTrace();
}
return BlogResult.error("日志查询失败");
}
}
3,服务层,关键!!!
这边返回的是一个div ,字体颜色,层的宽度和高度
可自行调整,username password 你Linux的连接名
这个命令最终对应了
String execute = “tail -n 你的行数 /home/web/test.log | grep ‘关键字’
BlogConfiguration 这个对象 对应颜色,可自行调整
package com.view.util;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import com.view.bean.BlogConfiguration;
import com.view.service.BlogConfigurationService;
import lombok.extern.slf4j.Slf4j;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.concurrent.TimeUnit;
@Slf4j
@Component
public class LogUtil {
private static String hostname;
private static int port;
private static String username;
private static String password;
private static String logAddress;
@Value("${logConfigurable.hostname}")
public void setHostname(String hostname) {
LogUtil.hostname = hostname;
}
@Value("${logConfigurable.port}")
public void setPort(int port) {
LogUtil.port = port;
}
@Value("${logConfigurable.username}")
public void setUsername(String username) {
LogUtil.username = username;
}
@Value("${logConfigurable.password}")
public void setPassword(String password) {
LogUtil.password = password;
}
@Value("${logConfigurable.logAddress}")
public void setLogAddress(String logAddress) {
LogUtil.logAddress = logAddress;
}
public static String tail(int row, String key,BlogConfiguration blogConfiguration) throws IOException {
final SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(new PromiscuousVerifier());
ssh.connect(hostname, port);
ssh.authPassword(username, password);
try (Session session = ssh.startSession()) {
String execute = "tail -n " + row + " "+logAddress+" | grep '" + (StrUtil.isEmpty(key) ? "" : key) + "'";
final Session.Command cmd = session.exec(execute);
InputStream inputStream = cmd.getInputStream();
OutputStream outputStream = IOUtils.readFully(inputStream);
int year = DateUtil.year(new Date());
String replaceAndBr = "<div style='width:100%;height:auto;position: absolute;left: 0px;top:0px;padding-left:15px;" +
"background-color:"+blogConfiguration.getLogBgColor()+";color:"+blogConfiguration.getLogFontColor()+";'>"
+outputStream.toString().replaceAll(year + "-", "<br/>" + year + "-")+"</div>";
return StrUtil.isEmpty(key)?replaceAndBr:replaceAndBr
.replaceAll(key,"<span style='border:1px solid "+blogConfiguration.getLogFontKeyColor()+";'>"+key+"</span>");
} finally {
// 处理异常
ssh.disconnect();
}
}
}
4,网页效果图
根据关键字检索
完工!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/112566.html