diff --git a/pom.xml b/pom.xml index b3cc0b2..b47b826 100644 --- a/pom.xml +++ b/pom.xml @@ -180,15 +180,9 @@ - org.apache.commons - commons-exec - 1.3 - - - - com.jcraft + com.github.mwiede jsch - 0.1.55 + 0.2.16 diff --git a/win-admin/src/main/java/com/win/web/controller/base/LinuxAccessController.java b/win-admin/src/main/java/com/win/web/controller/base/LinuxAccessController.java index addca37..fb77afb 100644 --- a/win-admin/src/main/java/com/win/web/controller/base/LinuxAccessController.java +++ b/win-admin/src/main/java/com/win/web/controller/base/LinuxAccessController.java @@ -1,8 +1,5 @@ package com.win.web.controller.base; -import java.time.LocalDateTime; -import java.util.List; - import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.win.common.annotation.Log; import com.win.common.core.controller.BaseController; @@ -15,18 +12,13 @@ import com.win.system.domain.LinuxAccess; import com.win.system.service.LinuxAccessService; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import javax.validation.Validator; +import java.time.LocalDateTime; +import java.util.List; /** * 访问控制Controller @@ -53,13 +45,13 @@ public class LinuxAccessController extends BaseController { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.isNull("delete_time"); if(linuxaccess.getCompanyCode() != null && !linuxaccess.getCompanyCode().isEmpty()) { - queryWrapper.eq("company_code", linuxaccess.getCompanyCode()); + queryWrapper.like("company_code", linuxaccess.getCompanyCode()); } if(linuxaccess.getCompanyName() != null && !linuxaccess.getCompanyName().isEmpty()) { queryWrapper.like("company_name", linuxaccess.getCompanyName()); } if(linuxaccess.getUri() != null && !linuxaccess.getUri().isEmpty()) { - queryWrapper.eq("uri", linuxaccess.getUri()); + queryWrapper.like("uri", linuxaccess.getUri()); } List list = linuxAccessService.list(queryWrapper); return getDataTable(list); @@ -74,13 +66,13 @@ public class LinuxAccessController extends BaseController { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.isNull("delete_time"); if(linuxAccess.getCompanyCode() != null && !linuxAccess.getCompanyCode().isEmpty()) { - queryWrapper.eq("company_code", linuxAccess.getCompanyCode()); + queryWrapper.like("company_code", linuxAccess.getCompanyCode()); } if(linuxAccess.getCompanyName() != null && !linuxAccess.getCompanyName().isEmpty()) { queryWrapper.like("company_name", linuxAccess.getCompanyName()); } if(linuxAccess.getUri() != null && !linuxAccess.getUri().isEmpty()) { - queryWrapper.eq("uri", linuxAccess.getUri()); + queryWrapper.like("uri", linuxAccess.getUri()); } List list = linuxAccessService.list(queryWrapper); ExcelUtil util = new ExcelUtil<>(LinuxAccess.class); diff --git a/win-common/pom.xml b/win-common/pom.xml index 28c43b4..a6eb2b2 100644 --- a/win-common/pom.xml +++ b/win-common/pom.xml @@ -142,12 +142,7 @@ - org.apache.commons - commons-exec - - - - com.jcraft + com.github.mwiede jsch diff --git a/win-common/src/main/java/com/win/common/utils/command/CommandUtil.java b/win-common/src/main/java/com/win/common/utils/command/CommandUtil.java index c2872ba..6d47aa5 100644 --- a/win-common/src/main/java/com/win/common/utils/command/CommandUtil.java +++ b/win-common/src/main/java/com/win/common/utils/command/CommandUtil.java @@ -1,34 +1,40 @@ package com.win.common.utils.command; -import org.apache.commons.exec.CommandLine; -import org.apache.commons.exec.DefaultExecutor; -import org.apache.commons.exec.PumpStreamHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -import java.io.ByteArrayOutputStream; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStreamReader; +/** + * 执行系统命令工具类 + */ public class CommandUtil { + private static Logger logger = LoggerFactory.getLogger(CommandUtil.class.getSimpleName()); + /** - * 执行系统命令 + * 执行指定命令 + * * @param command 命令 - * @throws IOException - * @throws InterruptedException + * @return 命令执行完成返回结果 + * @throws RuntimeException 失败时抛出异常,由调用者捕获处理 */ - public static String execute(String command) throws IOException, InterruptedException { - // 构建CommandLine对象 - CommandLine commandLine = CommandLine.parse(command); - // 构建DefaultExecutor对象 - DefaultExecutor defaultExecutor = new DefaultExecutor(); - // 构建ByteArrayOutputStream对象 - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - // 构建PumpStreamHandler对象 - PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(byteArrayOutputStream); - defaultExecutor.setStreamHandler(pumpStreamHandler); - // 开始执行命令 - defaultExecutor.execute(commandLine); - // 返回Linux命令执行的结果 - return byteArrayOutputStream.toString(); + public synchronized static String exeCommand(String command) throws RuntimeException { + logger.info("开始执行命令: " + command); + try { + Process process = Runtime.getRuntime().exec(command); + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line; + StringBuilder result = new StringBuilder(); + while ((line = reader.readLine()) != null) { + result.append(line).append("\n"); + } + return result.toString(); + } catch (IOException e) { + throw new RuntimeException(e); + } } -} +} \ No newline at end of file diff --git a/win-system/src/main/java/com/win/system/service/LinuxAccessService.java b/win-system/src/main/java/com/win/system/service/LinuxAccessService.java index 4c7d99a..6747dd7 100644 --- a/win-system/src/main/java/com/win/system/service/LinuxAccessService.java +++ b/win-system/src/main/java/com/win/system/service/LinuxAccessService.java @@ -19,6 +19,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.io.FileOutputStream; import java.io.IOException; import java.time.LocalDateTime; @@ -84,6 +85,7 @@ public class LinuxAccessService extends ServiceImpl qadAccessQueryWrapper = new QueryWrapper<>(); + qadAccessQueryWrapper.eq("company_code", companyCode); qadAccessQueryWrapper.eq("domain", domain); QadAccess qadAccess = qadAccessMapper.selectOne(qadAccessQueryWrapper); if(qadAccess == null) { @@ -93,9 +95,10 @@ public class LinuxAccessService extends ServiceImpl" + linuxAccess.getWorkPath() + traceid + ".json"); @@ -141,7 +144,7 @@ public class LinuxAccessService extends ServiceImpl qadAccessQueryWrapper = new QueryWrapper<>(); + qadAccessQueryWrapper.eq("company_code", companyCode); qadAccessQueryWrapper.eq("domain", domain); QadAccess qadAccess = qadAccessMapper.selectOne(qadAccessQueryWrapper); if(qadAccess == null) { @@ -166,18 +170,19 @@ public class LinuxAccessService extends ServiceImpl" + linuxAccess.getWorkPath() + traceid + ".json"); - log.info("write json result : {}", result); + FileOutputStream fileOutputStream = new FileOutputStream(linuxAccess.getWorkPath() + traceid + ".json"); + fileOutputStream.write(inJson.getBytes()); + fileOutputStream.close(); //写入dat.json String datJson = "\"" + linuxAccess.getWorkPath() + traceid + ".json\" \"" + linuxAccess.getQadProgramCode() + "\" \"" + linuxAccess.getOutPath() + traceid+".out.log\" \"" + qadAccess.getUserName() + "\" \"" + qadAccess.getPassword() + "\"\r\n\"" + domain + "\""; - log.info("datJson: {}", datJson); - result = CommandUtil.execute("touch " + linuxAccess.getWorkPath() + traceid + ".dat && echo '" + datJson + "'>" + linuxAccess.getWorkPath() + traceid+".dat"); - log.info("write dat json result : {}", result); + fileOutputStream = new FileOutputStream(linuxAccess.getWorkPath() + traceid + ".dat"); + fileOutputStream.write(datJson.getBytes()); + fileOutputStream.close(); //写入执行文件 String pJson = "output to " + linuxAccess.getOutPath() + traceid + ".run.log.\r\n" + "input from " + linuxAccess.getWorkPath() + traceid + ".dat.\r\n" + @@ -185,20 +190,22 @@ public class LinuxAccessService extends ServiceImpl" + linuxAccess.getWorkPath() + traceid + ".p"); - log.info("write p result : {}", result); + fileOutputStream = new FileOutputStream(linuxAccess.getWorkPath() + traceid + ".p"); + fileOutputStream.write(pJson.getBytes()); + fileOutputStream.close(); + log.info("pJson: {}", pJson); //执行QAD脚本 log.info("command : {}", linuxAccess.getScriptPath() + linuxAccess.getScriptName() + " " + linuxAccess.getWorkPath() + traceid + ".p"); - result = CommandUtil.execute(linuxAccess.getScriptPath() + linuxAccess.getScriptName() + " " + linuxAccess.getWorkPath() + traceid + ".p"); + String result = CommandUtil.exeCommand(linuxAccess.getScriptPath() + linuxAccess.getScriptName() + " " + linuxAccess.getWorkPath() + traceid + ".p"); log.info("execute {} result : {}", linuxAccess.getScriptPath() + linuxAccess.getScriptName(), result); - result = CommandUtil.execute("cat " + linuxAccess.getOutPath() + traceid + ".run.out.json"); + result = CommandUtil.exeCommand("cat " + linuxAccess.getOutPath() + traceid + ".run.out.json"); log.info("cat execute log : {}", result); if(result.indexOf("\"SUCCESS\"") > 0) { //移动执行结果文件 - CommandUtil.execute("mv " + linuxAccess.getOutPath() + traceid + ".run.out.json " + linuxAccess.getSuccessPath() + traceid + ".run.out.json"); - return AjaxResult.success(traceid); + CommandUtil.exeCommand("mv " + linuxAccess.getOutPath() + traceid + ".run.out.json " + linuxAccess.getSuccessPath() + traceid + ".run.out.json"); + return AjaxResult.success(result); } - CommandUtil.execute("mv " + linuxAccess.getOutPath() + traceid + ".run.out.json " + linuxAccess.getErrorPath() + traceid + ".run.out.json"); + CommandUtil.exeCommand("mv " + linuxAccess.getOutPath() + traceid + ".run.out.json " + linuxAccess.getErrorPath() + traceid + ".run.out.json"); return AjaxResult.error(HttpStatus.EXECUTE_FAIL, result); }