作者 朱兆平

在线执行CMD命令运维级别处理

... ... @@ -14,6 +14,9 @@ spring:
# view:
# suffix: .html
## prefix: /templates/
thymeleaf:
cache: false
mode: LEGACYHTML5
resources:
static-locations: classpath:/META-INF/resources/,classpath:/static,classpath:/resources/,classpath:/public/,file:${web.upload-path}
... ...
... ... @@ -45,6 +45,12 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--配合thymeleaf的LEGACYHTML5需要搭配一个额外的库NekoHTML才可用。-->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
... ...
... ... @@ -27,3 +27,4 @@
* 要集成util项目为model才能跑起来 [git地址](git@118.31.66.166:wlxxpt/utitls.git)
* 集成thymeleaf,webmvc 视图配置参考资料[地址](https://blog.csdn.net/zheng_chang_wei/article/details/76155440)
[打jar包后找不到路径](https://blog.csdn.net/qq_37372909/article/details/84824805)
* 支持在线CMD功能,运维级,访问路径/cmd
\ No newline at end of file
... ...
package com.tianbo.analysis.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;
@Controller
@Slf4j
public class CmdController {
@RequestMapping("/linuxCmd")
public String linuxCmd(@RequestParam(value = "command",required = false) String command,Model model){
List<String> strList = new ArrayList();
try {
if (command !=null && !command.isEmpty()){
Process process;
process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", command}, null, null);
InputStreamReader ir = new InputStreamReader(process
.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
process.waitFor();
while ((line = input.readLine()) != null) {
strList.add(line);
log.info(line);
}
model.addAttribute("cmdResult",strList);
}
}catch (Exception e){
e.printStackTrace();
log.error("执行命令出错",e);
}
return "cmd/cmd";
}
@RequestMapping("/windowsCmd")
public String windowsCmd(@RequestParam(value = "command",required = false) String command,Model model){
List<String> strList = new ArrayList();
try {
if (command !=null && !command.isEmpty()){
Runtime rt = Runtime.getRuntime();
String[] args = new String[]{"cmd","/c",command};
Process pr = rt.exec(args,null,null);
//Process pr = rt.exec("cmd /c dir"); // cmd /c calc
// Process pr = rt.exec("D:\\xunlei\\project.aspx");
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream(), "GBK"));
String line;
while ((line = input.readLine()) != null) {
strList.add(line);
log.info(line);
}
int exitVal = pr.waitFor();
log.info("exitVal",exitVal);
model.addAttribute("cmdResult",strList);
}
}catch (Exception e){
e.printStackTrace();
log.error("执行命令出错",e);
}
return "cmd/wincmd";
}
}
... ...
... ... @@ -39,7 +39,7 @@ public class TaskAnalysis {
String today = DateUtil.getTodayBy_yyyyMMdd();
String readDir = receptDir + "/" + today;
log.info("回执读取目录:"+readDir);
String backdireByDay = bakupDir + "/" + today;
try{
... ... @@ -77,6 +77,7 @@ public class TaskAnalysis {
String errDirByDay = errBakDir + "/" + today;
File berrDirectory = new File(errDirByDay);
FileUtils.moveFileToDirectory(file,berrDirectory,true);
log.error("解析或其他错误备份文件:"+File.pathSeparator);
}catch (Exception e){
e.printStackTrace();
}
... ...
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>新舱单在线运维</title>
</head>
<body>
<div>
<p>舱单回执目录: D:\Data\Receive</p>
<p>单一窗口回执目录: D:\TCSSingleWindow\recive</p>
<p>单一窗口报文转换工具目录:D:\TCSSingleWindow\报文转换工具,"java -Dfile.encoding=utf-8 -jar convert.jar"</p>
</div>
<div id="app">
<div>执行结果</div>
<!--/*@thymesVar id="cmdResult" type="java.util.List"*/-->
<div th:each="resoult : ${cmdResult}">
<p th:text="${resoult}"></p>
</div>
</div>
</body>
</html>
... ...
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="GBK">
<title>新舱单在线运维</title>
</head>
<body>
<div>
<p>舱单回执目录: D:\Data\Receive</p>
<p>单一窗口回执目录: D:\TCSSingleWindow\recive</p>
<p>单一窗口报文转换工具目录:D:\TCSSingleWindow\报文转换工具,"java -Dfile.encoding=utf-8 -jar convert.jar"</p>
<p>结束任务:taskkill</p>
</div>
<div id="app">
<div>执行结果</div>
<!--/*@thymesVar id="cmdResult" type="java.util.List"*/-->
<div th:each="resoult : ${cmdResult}">
<p th:text="${resoult}"></p>
</div>
</div>
</body>
</html>
... ...