作者 朱兆平

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

@@ -14,6 +14,9 @@ spring: @@ -14,6 +14,9 @@ spring:
14 # view: 14 # view:
15 # suffix: .html 15 # suffix: .html
16 ## prefix: /templates/ 16 ## prefix: /templates/
  17 + thymeleaf:
  18 + cache: false
  19 + mode: LEGACYHTML5
17 20
18 resources: 21 resources:
19 static-locations: classpath:/META-INF/resources/,classpath:/static,classpath:/resources/,classpath:/public/,file:${web.upload-path} 22 static-locations: classpath:/META-INF/resources/,classpath:/static,classpath:/resources/,classpath:/public/,file:${web.upload-path}
@@ -45,6 +45,12 @@ @@ -45,6 +45,12 @@
45 <groupId>org.springframework.boot</groupId> 45 <groupId>org.springframework.boot</groupId>
46 <artifactId>spring-boot-starter-thymeleaf</artifactId> 46 <artifactId>spring-boot-starter-thymeleaf</artifactId>
47 </dependency> 47 </dependency>
  48 + <!--配合thymeleaf的LEGACYHTML5需要搭配一个额外的库NekoHTML才可用。-->
  49 + <dependency>
  50 + <groupId>net.sourceforge.nekohtml</groupId>
  51 + <artifactId>nekohtml</artifactId>
  52 + <version>1.9.22</version>
  53 + </dependency>
48 <dependency> 54 <dependency>
49 <groupId>org.springframework.boot</groupId> 55 <groupId>org.springframework.boot</groupId>
50 <artifactId>spring-boot-starter-test</artifactId> 56 <artifactId>spring-boot-starter-test</artifactId>
@@ -26,4 +26,5 @@ @@ -26,4 +26,5 @@
26 * 打包jar散包 看这里 https://blog.csdn.net/m0_37202351/article/details/81738357 26 * 打包jar散包 看这里 https://blog.csdn.net/m0_37202351/article/details/81738357
27 * 要集成util项目为model才能跑起来 [git地址](git@118.31.66.166:wlxxpt/utitls.git) 27 * 要集成util项目为model才能跑起来 [git地址](git@118.31.66.166:wlxxpt/utitls.git)
28 * 集成thymeleaf,webmvc 视图配置参考资料[地址](https://blog.csdn.net/zheng_chang_wei/article/details/76155440) 28 * 集成thymeleaf,webmvc 视图配置参考资料[地址](https://blog.csdn.net/zheng_chang_wei/article/details/76155440)
29 -[打jar包后找不到路径](https://blog.csdn.net/qq_37372909/article/details/84824805)  
  29 +[打jar包后找不到路径](https://blog.csdn.net/qq_37372909/article/details/84824805)
  30 +* 支持在线CMD功能,运维级,访问路径/cmd
  1 +package com.tianbo.analysis.controller;
  2 +
  3 +import lombok.extern.slf4j.Slf4j;
  4 +import org.springframework.stereotype.Controller;
  5 +import org.springframework.ui.Model;
  6 +import org.springframework.web.bind.annotation.RequestMapping;
  7 +import org.springframework.web.bind.annotation.RequestParam;
  8 +
  9 +import java.io.BufferedReader;
  10 +import java.io.InputStreamReader;
  11 +import java.io.LineNumberReader;
  12 +import java.util.ArrayList;
  13 +import java.util.List;
  14 +
  15 +@Controller
  16 +@Slf4j
  17 +public class CmdController {
  18 +
  19 + @RequestMapping("/linuxCmd")
  20 + public String linuxCmd(@RequestParam(value = "command",required = false) String command,Model model){
  21 + List<String> strList = new ArrayList();
  22 + try {
  23 + if (command !=null && !command.isEmpty()){
  24 + Process process;
  25 + process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", command}, null, null);
  26 + InputStreamReader ir = new InputStreamReader(process
  27 + .getInputStream());
  28 + LineNumberReader input = new LineNumberReader(ir);
  29 + String line;
  30 + process.waitFor();
  31 + while ((line = input.readLine()) != null) {
  32 + strList.add(line);
  33 + log.info(line);
  34 + }
  35 + model.addAttribute("cmdResult",strList);
  36 + }
  37 + }catch (Exception e){
  38 + e.printStackTrace();
  39 + log.error("执行命令出错",e);
  40 + }
  41 +
  42 +
  43 +
  44 + return "cmd/cmd";
  45 + }
  46 +
  47 + @RequestMapping("/windowsCmd")
  48 + public String windowsCmd(@RequestParam(value = "command",required = false) String command,Model model){
  49 + List<String> strList = new ArrayList();
  50 + try {
  51 + if (command !=null && !command.isEmpty()){
  52 + Runtime rt = Runtime.getRuntime();
  53 + String[] args = new String[]{"cmd","/c",command};
  54 + Process pr = rt.exec(args,null,null);
  55 + //Process pr = rt.exec("cmd /c dir"); // cmd /c calc
  56 + // Process pr = rt.exec("D:\\xunlei\\project.aspx");
  57 +
  58 + BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream(), "GBK"));
  59 +
  60 + String line;
  61 +
  62 + while ((line = input.readLine()) != null) {
  63 + strList.add(line);
  64 + log.info(line);
  65 + }
  66 +
  67 + int exitVal = pr.waitFor();
  68 + log.info("exitVal",exitVal);
  69 + model.addAttribute("cmdResult",strList);
  70 + }
  71 + }catch (Exception e){
  72 + e.printStackTrace();
  73 + log.error("执行命令出错",e);
  74 + }
  75 +
  76 +
  77 +
  78 + return "cmd/wincmd";
  79 + }
  80 +}
@@ -39,7 +39,7 @@ public class TaskAnalysis { @@ -39,7 +39,7 @@ public class TaskAnalysis {
39 String today = DateUtil.getTodayBy_yyyyMMdd(); 39 String today = DateUtil.getTodayBy_yyyyMMdd();
40 String readDir = receptDir + "/" + today; 40 String readDir = receptDir + "/" + today;
41 41
42 - log.info("回执读取目录:"+readDir); 42 +
43 String backdireByDay = bakupDir + "/" + today; 43 String backdireByDay = bakupDir + "/" + today;
44 44
45 try{ 45 try{
@@ -77,6 +77,7 @@ public class TaskAnalysis { @@ -77,6 +77,7 @@ public class TaskAnalysis {
77 String errDirByDay = errBakDir + "/" + today; 77 String errDirByDay = errBakDir + "/" + today;
78 File berrDirectory = new File(errDirByDay); 78 File berrDirectory = new File(errDirByDay);
79 FileUtils.moveFileToDirectory(file,berrDirectory,true); 79 FileUtils.moveFileToDirectory(file,berrDirectory,true);
  80 + log.error("解析或其他错误备份文件:"+File.pathSeparator);
80 }catch (Exception e){ 81 }catch (Exception e){
81 e.printStackTrace(); 82 e.printStackTrace();
82 } 83 }
  1 +<!DOCTYPE html>
  2 +<html lang="en" xmlns:th="http://www.thymeleaf.org">
  3 +<head>
  4 + <meta charset="UTF-8">
  5 + <title>新舱单在线运维</title>
  6 +</head>
  7 +<body>
  8 +<div>
  9 + <p>舱单回执目录: D:\Data\Receive</p>
  10 + <p>单一窗口回执目录: D:\TCSSingleWindow\recive</p>
  11 + <p>单一窗口报文转换工具目录:D:\TCSSingleWindow\报文转换工具,"java -Dfile.encoding=utf-8 -jar convert.jar"</p>
  12 +</div>
  13 +<div id="app">
  14 + <div>执行结果</div>
  15 + <!--/*@thymesVar id="cmdResult" type="java.util.List"*/-->
  16 + <div th:each="resoult : ${cmdResult}">
  17 + <p th:text="${resoult}"></p>
  18 + </div>
  19 +</div>
  20 +</body>
  21 +
  22 +</html>
  1 +<!DOCTYPE html>
  2 +<html lang="en" xmlns:th="http://www.thymeleaf.org">
  3 +<head>
  4 + <meta charset="GBK">
  5 + <title>新舱单在线运维</title>
  6 +</head>
  7 +<body>
  8 +<div>
  9 + <p>舱单回执目录: D:\Data\Receive</p>
  10 + <p>单一窗口回执目录: D:\TCSSingleWindow\recive</p>
  11 + <p>单一窗口报文转换工具目录:D:\TCSSingleWindow\报文转换工具,"java -Dfile.encoding=utf-8 -jar convert.jar"</p>
  12 + <p>结束任务:taskkill</p>
  13 +</div>
  14 +<div id="app">
  15 + <div>执行结果</div>
  16 + <!--/*@thymesVar id="cmdResult" type="java.util.List"*/-->
  17 + <div th:each="resoult : ${cmdResult}">
  18 + <p th:text="${resoult}"></p>
  19 + </div>
  20 +</div>
  21 +</body>
  22 +
  23 +</html>