Browse Source

完成对接qad

master
liuchen864 9 months ago
parent
commit
84fc3bcb01
  1. 65
      win-admin/src/main/java/com/win/web/controller/base/CommandController.java
  2. 2
      win-admin/src/main/java/com/win/web/controller/base/LinuxAccessController.java
  3. 70
      win-admin/src/main/java/com/win/web/controller/base/ShellController.java
  4. 20
      win-common/src/main/java/com/win/common/constant/HttpStatus.java
  5. 42
      win-common/src/main/java/com/win/common/utils/shell/ShellUtil.java
  6. 13
      win-framework/src/main/java/com/win/framework/aspectj/LogAspect.java
  7. 2
      win-system/src/main/java/com/win/system/domain/LinuxAccess.java
  8. 135
      win-system/src/main/java/com/win/system/service/LinuxAccessService.java

65
win-admin/src/main/java/com/win/web/controller/base/CommandController.java

@ -1,11 +1,14 @@
package com.win.web.controller.base; package com.win.web.controller.base;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.win.common.annotation.Log;
import com.win.common.constant.HttpStatus; import com.win.common.constant.HttpStatus;
import com.win.common.core.domain.AjaxResult; import com.win.common.core.domain.AjaxResult;
import com.win.common.enums.BusinessType;
import com.win.common.utils.ProfileUtil; import com.win.common.utils.ProfileUtil;
import com.win.common.utils.command.CommandUtil; import com.win.common.utils.command.CommandUtil;
import com.win.common.utils.shell.ShellVo; import com.win.common.utils.shell.ShellVo;
import com.win.system.service.LinuxAccessService;
import com.win.web.config.ShellConfig; import com.win.web.config.ShellConfig;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -32,7 +35,7 @@ public class CommandController {
private static final Logger logger = LoggerFactory.getLogger("sys-user"); private static final Logger logger = LoggerFactory.getLogger("sys-user");
@Autowired @Autowired
private ShellConfig shellConfig; private LinuxAccessService linuxAccessService;
/** /**
* 对外提供一个接口通过header中的interfaceName反射机制调用方法方法必须写到这个controller中并且不用加PostMapping注解 * 对外提供一个接口通过header中的interfaceName反射机制调用方法方法必须写到这个controller中并且不用加PostMapping注解
@ -42,6 +45,7 @@ public class CommandController {
* @return 结果 * @return 结果
*/ */
@PostMapping("/api") @PostMapping("/api")
@Log(title = "命令行对接qad", businessType = BusinessType.INSERT, isManager = false)
public AjaxResult api(HttpServletRequest request, @RequestBody String body) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { public AjaxResult api(HttpServletRequest request, @RequestBody String body) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
String interfaceName = request.getHeader("interface"); String interfaceName = request.getHeader("interface");
String sign = request.getHeader("sign"); String sign = request.getHeader("sign");
@ -55,7 +59,7 @@ public class CommandController {
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
return AjaxResult.error(HttpStatus.TIMESTAMP_ERROR, "时间戳不正确"); return AjaxResult.error(HttpStatus.TIMESTAMP_ERROR, "时间戳不正确");
} }
String tmp = interfaceName + body + timestamp; String tmp = interfaceName + body + "Wenyin@2024" + timestamp;
String computeSign = DigestUtils.md5DigestAsHex(tmp.getBytes()); String computeSign = DigestUtils.md5DigestAsHex(tmp.getBytes());
long tenTimestamp = timestamp + (10 * 60 * 1000); // 计算10分钟后的时间戳 long tenTimestamp = timestamp + (10 * 60 * 1000); // 计算10分钟后的时间戳
long currentTimestamp = System.currentTimeMillis(); // 获取当前时间戳 long currentTimestamp = System.currentTimeMillis(); // 获取当前时间戳
@ -72,61 +76,18 @@ public class CommandController {
return (AjaxResult) method.invoke(this, body); return (AjaxResult) method.invoke(this, body);
} }
public AjaxResult itemmastercreate(String body) throws IOException, InterruptedException {
String result = CommandUtil.execute("cmd /c ping www.baidu.com -n 5");
return AjaxResult.success(result);
}
public AjaxResult itemdetailcreate(String body) throws IOException, InterruptedException {
String result = CommandUtil.execute("cmd dir");
return AjaxResult.success(result);
}
public AjaxResult discretepo(String body) throws IOException, InterruptedException { public AjaxResult discretepo(String body) throws IOException, InterruptedException {
JSONObject jsonObject = JSONObject.parseObject(body); JSONObject jsonObject = JSONObject.parseObject(body);
jsonObject = JSONObject.parseObject(jsonObject.getString("dsDescreteOrder")); jsonObject = JSONObject.parseObject(jsonObject.getString("dsDescreteOrder"));
ShellVo shellVo; String domain = jsonObject.getString("domain");
if(jsonObject.getString("domain") != null && jsonObject.getString("domain").equals("JLHT2")) { if(domain == null || domain.isEmpty()) {
shellVo = shellConfig.getJlht2(); return AjaxResult.error(HttpStatus.DOMAIN_ERROR, "域不能为空");
} else {
shellVo = shellConfig.getJlht();
} }
//生成traceid String companyCode = jsonObject.getString("company_code");
String traceid = jsonObject.getString("traceid"); if(companyCode == null || companyCode.isEmpty()) {
jsonObject.remove("traceid"); return AjaxResult.error(HttpStatus.COMPANY_CODE_ERROR, "公司编码不能为空");
jsonObject.remove("company");
jsonObject.remove("domain");
String inJson = "{\"dsDescreteOrder\":"+ jsonObject+"}";
log.info("inJson: {}", inJson);
//写入json
String result = CommandUtil.execute("touch /home/mfg/work/cim/po/"+traceid+".json && echo '"+inJson+"'>/home/mfg/work/cim/po/"+traceid+".json");
log.info("write json result : " + result);
//写入dat.json
String datJson = "\"/home/mfg/work/cim/po/"+traceid+".json\" \"xxdspoapi.p\" \"/home/mfg/work/out/po/"+traceid+".out.log\" \""+shellVo.getQadUser()+"\" \""+shellVo.getQadPassword()+"\"\r\n\""+shellVo.getQadDomain()+"\"";
log.info("datJson: {}", datJson);
result = CommandUtil.execute("touch /home/mfg/work/cim/po/"+traceid+".dat && echo '"+datJson+"'>/home/mfg/work/cim/po/"+traceid+".dat");
log.info("write dat json result : " + result);
//写入执行文件
String pJson = "output to /home/mfg/work/out/po/" + traceid + ".run.log.\r\n" +
"input from /home/mfg/work/cim/po/" + traceid + ".dat.\r\n" +
"run /home/mfg/work/scripts/xxcommon01.p.\r\n" +
"input close.\r\n" +
"output close.";
log.info("pJson: {}", pJson);
result = CommandUtil.execute("touch /home/mfg/work/cim/po/"+traceid+".p && echo '"+pJson+"'>/home/mfg/work/cim/po/"+traceid+".p");
log.info("write p result : " + result);
//执行QAD脚本
result = CommandUtil.execute("sh /home/mfg/work/scripts/client-ch-cimload /home/mfg/work/cim/po/"+traceid+".p");
log.info("execute client-ch-loaddata : " + result);
result = CommandUtil.execute("cat /home/mfg/work/out/po/" + traceid + ".run.out.json");
log.info("cat execute log : " + result);
if(result.indexOf("\"SUCCESS\"") > 0) {
//移动执行结果文件
CommandUtil.execute("mv /home/mfg/work/out/po/" + traceid + ".run.out.json /home/mfg/work/success/po/" + traceid + ".run.out.json");
return AjaxResult.success(traceid);
} }
CommandUtil.execute("mv /home/mfg/work/out/po/" + traceid + ".run.out.json /home/mfg/work/err/po/" + traceid + ".run.out.json"); return linuxAccessService.executeCommand(domain, companyCode, "purchaseorder/discretepo/", jsonObject);
return AjaxResult.error(HttpStatus.EXECUTE_FAIL, result);
} }
} }

2
win-admin/src/main/java/com/win/web/controller/base/LinuxAccessController.java

@ -141,7 +141,7 @@ public class LinuxAccessController extends BaseController {
@Log(title = "访问控制", businessType = BusinessType.DELETE) @Log(title = "访问控制", businessType = BusinessType.DELETE)
@DeleteMapping("/{ids}") @DeleteMapping("/{ids}")
public AjaxResult batchRemove(@PathVariable Long[] ids) { public AjaxResult batchRemove(@PathVariable Long[] ids) {
return toAjax(linuxAccessService.deleteLinuxAccessByGuids(ids, getUserId())); return toAjax(linuxAccessService.deleteLinuxAccessByIds(ids, getUserId()));
} }
} }

70
win-admin/src/main/java/com/win/web/controller/base/ShellController.java

@ -3,11 +3,14 @@ package com.win.web.controller.base;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.jcraft.jsch.JSchException; import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session; import com.jcraft.jsch.Session;
import com.win.common.annotation.Log;
import com.win.common.constant.HttpStatus; import com.win.common.constant.HttpStatus;
import com.win.common.core.domain.AjaxResult; import com.win.common.core.domain.AjaxResult;
import com.win.common.enums.BusinessType;
import com.win.common.utils.ProfileUtil; import com.win.common.utils.ProfileUtil;
import com.win.common.utils.shell.ShellUtil; import com.win.common.utils.shell.ShellUtil;
import com.win.common.utils.shell.ShellVo; import com.win.common.utils.shell.ShellVo;
import com.win.system.service.LinuxAccessService;
import com.win.web.config.ShellConfig; import com.win.web.config.ShellConfig;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -34,9 +37,7 @@ public class ShellController {
private static final Logger logger = LoggerFactory.getLogger("sys-user"); private static final Logger logger = LoggerFactory.getLogger("sys-user");
@Autowired @Autowired
private ShellUtil shellUtil; private LinuxAccessService linuxAccessService;
@Autowired
private ShellConfig shellConfig;
/** /**
* 对外提供一个接口通过header中的interfaceName反射机制调用方法方法必须写到这个controller中并且不用加PostMapping注解 * 对外提供一个接口通过header中的interfaceName反射机制调用方法方法必须写到这个controller中并且不用加PostMapping注解
@ -46,6 +47,7 @@ public class ShellController {
* @return 结果 * @return 结果
*/ */
@PostMapping("/api") @PostMapping("/api")
@Log(title = "远程行对接qad", businessType = BusinessType.INSERT, isManager = false)
public AjaxResult api(HttpServletRequest request, @RequestBody String body) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { public AjaxResult api(HttpServletRequest request, @RequestBody String body) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
String interfaceName = request.getHeader("interface"); String interfaceName = request.getHeader("interface");
String sign = request.getHeader("sign"); String sign = request.getHeader("sign");
@ -59,7 +61,7 @@ public class ShellController {
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
return AjaxResult.error(HttpStatus.TIMESTAMP_ERROR, "时间戳不正确"); return AjaxResult.error(HttpStatus.TIMESTAMP_ERROR, "时间戳不正确");
} }
String tmp = interfaceName + body + timestamp; String tmp = interfaceName + body + "Wenyin@2024" + timestamp;
String computeSign = DigestUtils.md5DigestAsHex(tmp.getBytes()); String computeSign = DigestUtils.md5DigestAsHex(tmp.getBytes());
long tenTimestamp = timestamp + (10 * 60 * 1000); // 计算10分钟后的时间戳 long tenTimestamp = timestamp + (10 * 60 * 1000); // 计算10分钟后的时间戳
long currentTimestamp = System.currentTimeMillis(); // 获取当前时间戳 long currentTimestamp = System.currentTimeMillis(); // 获取当前时间戳
@ -76,64 +78,18 @@ public class ShellController {
return (AjaxResult) method.invoke(this, body); return (AjaxResult) method.invoke(this, body);
} }
public AjaxResult itemmastercreate(String body) throws JSchException, IOException {
Session session = shellUtil.getShellSession(shellConfig.getJlht2());
String result = shellUtil.execute(session, "cd /opt && ls -l");
return AjaxResult.success(result);
}
public AjaxResult itemdetailcreate(String body) throws JSchException, IOException {
Session session = shellUtil.getShellSession(shellConfig.getJlht2());
String result = shellUtil.execute(session, "cd /opt && ls -l");
return AjaxResult.success(result);
}
public AjaxResult discretepo(String body) throws JSchException, IOException { public AjaxResult discretepo(String body) throws JSchException, IOException {
JSONObject jsonObject = JSONObject.parseObject(body); JSONObject jsonObject = JSONObject.parseObject(body);
jsonObject = JSONObject.parseObject(jsonObject.getString("dsDescreteOrder")); jsonObject = JSONObject.parseObject(jsonObject.getString("dsDescreteOrder"));
ShellVo shellVo; String domain = jsonObject.getString("domain");
if(jsonObject.getString("domain") != null && jsonObject.getString("domain").equals("JLHT2")) { if(domain == null || domain.isEmpty()) {
shellVo = shellConfig.getJlht2(); return AjaxResult.error(HttpStatus.DOMAIN_ERROR, "域不能为空");
} else {
shellVo = shellConfig.getJlht();
} }
Session session = shellUtil.getShellSession(shellVo); String companyCode = jsonObject.getString("company_code");
//生成traceid if(companyCode == null || companyCode.isEmpty()) {
String traceid = jsonObject.getString("traceid"); return AjaxResult.error(HttpStatus.COMPANY_CODE_ERROR, "公司编码不能为空");
jsonObject.remove("traceid");
jsonObject.remove("company");
jsonObject.remove("domain");
String inJson = "{\"dsDescreteOrder\":"+ jsonObject+"}";
log.info("inJson: {}", inJson);
//写入json
String result = shellUtil.execute(session, "touch /home/mfg/work/cim/po/"+traceid+".json && echo '"+inJson+"'>/home/mfg/work/cim/po/"+traceid+".json");
log.info("write json result : " + result);
//写入dat.json
String datJson = "\"/home/mfg/work/cim/po/"+traceid+".json\" \"xxdspoapi.p\" \"/home/mfg/work/out/po/"+traceid+".out.log\" \""+shellVo.getQadUser()+"\" \""+shellVo.getQadPassword()+"\"\r\n\""+shellVo.getQadDomain()+"\"";
log.info("datJson: {}", datJson);
result = shellUtil.execute(session, "touch /home/mfg/work/cim/po/"+traceid+".dat && echo '"+datJson+"'>/home/mfg/work/cim/po/"+traceid+".dat");
log.info("write dat json result : " + result);
//写入执行文件
String pJson = "output to /home/mfg/work/out/po/" + traceid + ".run.log.\r\n" +
"input from /home/mfg/work/cim/po/" + traceid + ".dat.\r\n" +
"run /home/mfg/work/scripts/xxcommon01.p.\r\n" +
"input close.\r\n" +
"output close.";
log.info("pJson: {}", pJson);
result = shellUtil.execute(session, "touch /home/mfg/work/cim/po/"+traceid+".p && echo '"+pJson+"'>/home/mfg/work/cim/po/"+traceid+".p");
log.info("write p result : " + result);
//执行QAD脚本
result = shellUtil.execute(session, "/home/mfg/work/scripts/client-ch-cimload /home/mfg/work/cim/po/"+traceid+".p");
log.info("execute client-ch-loaddata : " + result);
result = shellUtil.execute(session, "cat /home/mfg/work/out/po/" + traceid + ".run.out.json");
log.info("cat execute log : " + result);
if(result.indexOf("\"SUCCESS\"") > 0) {
//移动执行结果文件
shellUtil.execute(session, "mv /home/mfg/work/out/po/" + traceid + ".run.out.json /home/mfg/work/success/po/" + traceid + ".run.out.json");
return AjaxResult.success(traceid);
} }
shellUtil.execute(session, "mv /home/mfg/work/out/po/" + traceid + ".run.out.json /home/mfg/work/err/po/" + traceid + ".run.out.json"); return linuxAccessService.executeShell(domain, companyCode, "purchaseorder/discretepo/", jsonObject);
return AjaxResult.error(HttpStatus.EXECUTE_FAIL, result);
} }
} }

20
win-common/src/main/java/com/win/common/constant/HttpStatus.java

@ -57,6 +57,26 @@ public class HttpStatus
*/ */
public static final int EXPIRE_ERROR = 307; public static final int EXPIRE_ERROR = 307;
/**
* 域不能为空
*/
public static final int DOMAIN_ERROR = 308;
/**
* 公司编码不能为空
*/
public static final int COMPANY_CODE_ERROR = 309;
/**
* linux访问控制不存在
*/
public static final int LINUX_ACCESS_ERROR = 310;
/**
* QAD访问控制不存在
*/
public static final int QAD_ACCESS_ERROR = 311;
/** /**
* 参数列表错误缺少格式不匹配 * 参数列表错误缺少格式不匹配
*/ */

42
win-common/src/main/java/com/win/common/utils/shell/ShellUtil.java

@ -21,15 +21,18 @@ public class ShellUtil {
/** /**
* 获取linux连接 * 获取linux连接
* @param shellVo * @param userName 用户名
* @return * @param password 密码
* @throws JSchException * @param ip ip
* @param port 端口
* @return 连接
* @throws JSchException 链接异常
*/ */
public Session getShellSession(ShellVo shellVo) throws JSchException { public Session getShellSession(String userName, String password, String ip, int port) throws JSchException {
Session session = this.sessionConcurrentHashMap.get(shellVo.getQadDomain()); Session session = this.sessionConcurrentHashMap.get(ip + "_" + userName);
if(session == null || !session.isConnected()) { if(session == null || !session.isConnected()) {
session = this.createSession(shellVo); session = this.createSession(userName, password, ip, port);
this.sessionConcurrentHashMap.put(shellVo.getQadDomain(), session); this.sessionConcurrentHashMap.put(ip + "_" + userName, session);
return session; return session;
} else { } else {
return session; return session;
@ -38,11 +41,11 @@ public class ShellUtil {
/** /**
* 执行linux命令 * 执行linux命令
* @param session * @param session 连接
* @param command * @param command 命令
* @return * @return 执行结果
* @throws JSchException * @throws JSchException 连接异常
* @throws IOException * @throws IOException io异常
*/ */
public String execute(Session session, String command) throws JSchException, IOException { public String execute(Session session, String command) throws JSchException, IOException {
// 创建SSH Channel // 创建SSH Channel
@ -72,16 +75,19 @@ public class ShellUtil {
/** /**
* 创建linux连接 * 创建linux连接
* @param shellVo * @param userName 用户名
* @return * @param password 密码
* @throws JSchException * @param ip ip
* @param port 端口
* @return 连接session
* @throws JSchException 连接异常
*/ */
protected Session createSession(ShellVo shellVo) throws JSchException { protected Session createSession(String userName, String password, String ip, int port) throws JSchException {
// 创建JSch实例 // 创建JSch实例
JSch jsch = new JSch(); JSch jsch = new JSch();
// 创建SSH Session // 创建SSH Session
Session session = jsch.getSession(shellVo.getLinuxId(), shellVo.getIp(), shellVo.getPort()); Session session = jsch.getSession(userName, ip, port);
session.setPassword(shellVo.getLinuxPassword()); session.setPassword(password);
// 禁用用户信息验证 // 禁用用户信息验证
java.util.Properties config = new java.util.Properties(); java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no"); config.put("StrictHostKeyChecking", "no");

13
win-framework/src/main/java/com/win/framework/aspectj/LogAspect.java

@ -66,9 +66,6 @@ public class LogAspect {
protected void handleLog(final JoinPoint joinPoint, Log controllerLog, final Exception e, Object jsonResult) { protected void handleLog(final JoinPoint joinPoint, Log controllerLog, final Exception e, Object jsonResult) {
try { try {
// 获取当前的用户
LoginUser loginUser = SecurityUtils.getLoginUser();
// *========数据库日志=========*// // *========数据库日志=========*//
SysOperLog operLog = new SysOperLog(); SysOperLog operLog = new SysOperLog();
operLog.setStatus(BusinessStatus.SUCCESS.ordinal()); operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
@ -76,10 +73,14 @@ public class LogAspect {
String ip = IpUtils.getIpAddr(ServletUtils.getRequest()); String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
operLog.setOperIp(ip); operLog.setOperIp(ip);
operLog.setOperUrl(StringUtils.substring(ServletUtils.getRequest().getRequestURI(), 0, 255)); operLog.setOperUrl(StringUtils.substring(ServletUtils.getRequest().getRequestURI(), 0, 255));
if (loginUser != null) { if(controllerLog.isManager()) {
operLog.setOperName(loginUser.getUsername()); // 获取当前的用户
} LoginUser loginUser = SecurityUtils.getLoginUser();
if (loginUser != null) {
operLog.setOperName(loginUser.getUsername());
}
}
if (e != null) { if (e != null) {
operLog.setStatus(BusinessStatus.FAIL.ordinal()); operLog.setStatus(BusinessStatus.FAIL.ordinal());
operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000)); operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));

2
win-system/src/main/java/com/win/system/domain/LinuxAccess.java

@ -35,7 +35,7 @@ public class LinuxAccess extends BaseEntity<LinuxAccess> {
private String ipAddress; private String ipAddress;
@Excel(name = "端口") @Excel(name = "端口")
private String port; private Integer port;
@Excel(name = "用户") @Excel(name = "用户")
private String userName; private String userName;

135
win-system/src/main/java/com/win/system/service/LinuxAccessService.java

@ -1,14 +1,18 @@
package com.win.system.service; package com.win.system.service;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jcraft.jsch.JSchException; import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session; import com.jcraft.jsch.Session;
import com.win.common.constant.HttpStatus; import com.win.common.constant.HttpStatus;
import com.win.common.core.domain.AjaxResult; import com.win.common.core.domain.AjaxResult;
import com.win.common.utils.command.CommandUtil;
import com.win.common.utils.shell.ShellUtil; import com.win.common.utils.shell.ShellUtil;
import com.win.common.utils.shell.ShellVo; import com.win.common.utils.shell.ShellVo;
import com.win.system.domain.QadAccess;
import com.win.system.mapper.QadAccessMapper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -31,6 +35,8 @@ public class LinuxAccessService extends ServiceImpl<LinuxAccessMapper, LinuxAcce
@Autowired @Autowired
private ShellUtil shellUtil; private ShellUtil shellUtil;
@Autowired
private QadAccessMapper qadAccessMapper;
/** /**
* 批量删除请填写功能名称 * 批量删除请填写功能名称
@ -39,7 +45,7 @@ public class LinuxAccessService extends ServiceImpl<LinuxAccessMapper, LinuxAcce
* @return 删除的数量 * @return 删除的数量
*/ */
@Transactional @Transactional
public int deleteLinuxAccessByGuids(Long[] ids, Long userId) { public int deleteLinuxAccessByIds(Long[] ids, Long userId) {
int result = 0; int result = 0;
for(Long id : ids) { for(Long id : ids) {
LinuxAccess linuxaccess = baseMapper.selectById(id); LinuxAccess linuxaccess = baseMapper.selectById(id);
@ -50,4 +56,131 @@ public class LinuxAccessService extends ServiceImpl<LinuxAccessMapper, LinuxAcce
return result; return result;
} }
/**
* 执行shell脚本
*
* @param domain
* @param companyCode 公司编码
* @param jsonObject 传输对象
* @return 执行结果
* @throws JSchException shell异常
* @throws IOException io异常
*/
public AjaxResult executeShell(String domain, String companyCode, String uri, JSONObject jsonObject) throws JSchException, IOException {
QueryWrapper<LinuxAccess> linuxAccessQueryWrapper = new QueryWrapper<>();
linuxAccessQueryWrapper.eq("company_code", companyCode);
linuxAccessQueryWrapper.eq("uri", uri);
LinuxAccess linuxAccess = baseMapper.selectOne(linuxAccessQueryWrapper);
if(linuxAccess == null) {
return AjaxResult.error(HttpStatus.LINUX_ACCESS_ERROR, "linux访问控制不存在");
}
QueryWrapper<QadAccess> qadAccessQueryWrapper = new QueryWrapper<>();
qadAccessQueryWrapper.eq("domain", domain);
QadAccess qadAccess = qadAccessMapper.selectOne(qadAccessQueryWrapper);
if(qadAccess == null) {
return AjaxResult.error(HttpStatus.QAD_ACCESS_ERROR, "QAD_ACCESS_ERROR");
}
Session session = shellUtil.getShellSession(linuxAccess.getUserName(), linuxAccess.getPassword(), linuxAccess.getIpAddress(), linuxAccess.getPort());
//生成traceid
String traceid = jsonObject.getString("traceid");
jsonObject.remove("traceid");
jsonObject.remove("company_code");
jsonObject.remove("domain");
String inJson = "{\"dsDescreteOrder\":"+ jsonObject+"}";
log.info("inJson: {}", inJson);
//写入json
String result = shellUtil.execute(session, "touch " + linuxAccess.getWorkPath() + traceid + ".json && echo '" + inJson + "'>" + linuxAccess.getWorkPath() + traceid + ".json");
log.info("write json result : {}", result);
//写入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 = shellUtil.execute(session, "touch " + linuxAccess.getWorkPath() + traceid + ".dat && echo '" + datJson + "'>" + linuxAccess.getWorkPath() + traceid+".dat");
log.info("write dat json result : {}", result);
//写入执行文件
String pJson = "output to " + linuxAccess.getOutPath() + traceid + ".run.log.\r\n" +
"input from " + linuxAccess.getWorkPath() + traceid + ".dat.\r\n" +
"run " + linuxAccess.getScriptPath() + linuxAccess.getScriptProgram() + ".\r\n" +
"input close.\r\n" +
"output close.";
log.info("pJson: {}", pJson);
result = shellUtil.execute(session, "touch " + linuxAccess.getWorkPath() + traceid+".p && echo '"+pJson+"'>" + linuxAccess.getWorkPath() + traceid + ".p");
log.info("write p result : {}", result);
//执行QAD脚本
log.info("command : {}", linuxAccess.getScriptPath() + linuxAccess.getScriptName() + " " + linuxAccess.getWorkPath() + traceid + ".p");
result = shellUtil.execute(session, linuxAccess.getScriptPath() + linuxAccess.getScriptName() + " " + linuxAccess.getWorkPath() + traceid + ".p");
log.info("execute {} result : {}", linuxAccess.getScriptPath() + linuxAccess.getScriptName(), result);
result = shellUtil.execute(session, "cat " + linuxAccess.getOutPath() + traceid + ".run.out.json");
log.info("cat execute log : {}", result);
if(result.indexOf("\"SUCCESS\"") > 0) {
//移动执行结果文件
shellUtil.execute(session, "mv " + linuxAccess.getOutPath() + traceid + ".run.out.json " + linuxAccess.getSuccessPath() + traceid + ".run.out.json");
return AjaxResult.success(traceid);
}
shellUtil.execute(session, "mv " + linuxAccess.getOutPath() + traceid + ".run.out.json " + linuxAccess.getErrorPath() + traceid + ".run.out.json");
return AjaxResult.error(HttpStatus.EXECUTE_FAIL, result);
}
/**
* 执行命令行
*
* @param domain
* @param companyCode 公司编码
* @param jsonObject 传输对象
* @return 执行结果
* @throws InterruptedException 命令行异常
* @throws IOException io异常
*/
public AjaxResult executeCommand(String domain, String companyCode, String uri, JSONObject jsonObject) throws IOException, InterruptedException {
QueryWrapper<LinuxAccess> linuxAccessQueryWrapper = new QueryWrapper<>();
linuxAccessQueryWrapper.eq("company_code", companyCode);
linuxAccessQueryWrapper.eq("uri", uri);
LinuxAccess linuxAccess = baseMapper.selectOne(linuxAccessQueryWrapper);
if(linuxAccess == null) {
return AjaxResult.error(HttpStatus.LINUX_ACCESS_ERROR, "linux访问控制不存在");
}
QueryWrapper<QadAccess> qadAccessQueryWrapper = new QueryWrapper<>();
qadAccessQueryWrapper.eq("domain", domain);
QadAccess qadAccess = qadAccessMapper.selectOne(qadAccessQueryWrapper);
if(qadAccess == null) {
return AjaxResult.error(HttpStatus.QAD_ACCESS_ERROR, "QAD_ACCESS_ERROR");
}
//生成traceid
String traceid = jsonObject.getString("traceid");
jsonObject.remove("traceid");
jsonObject.remove("company_code");
jsonObject.remove("domain");
String inJson = "{\"dsDescreteOrder\":"+ jsonObject+"}";
log.info("inJson: {}", inJson);
//写入json
String result = CommandUtil.execute("touch " + linuxAccess.getWorkPath() + traceid + ".json && echo '" + inJson + "'>" + linuxAccess.getWorkPath() + traceid + ".json");
log.info("write json result : {}", result);
//写入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);
//写入执行文件
String pJson = "output to " + linuxAccess.getOutPath() + traceid + ".run.log.\r\n" +
"input from " + linuxAccess.getWorkPath() + traceid + ".dat.\r\n" +
"run " + linuxAccess.getScriptPath() + linuxAccess.getScriptProgram() + ".\r\n" +
"input close.\r\n" +
"output close.";
log.info("pJson: {}", pJson);
result = CommandUtil.execute("touch " + linuxAccess.getWorkPath() + traceid+".p && echo '"+pJson+"'>" + linuxAccess.getWorkPath() + traceid + ".p");
log.info("write p result : {}", result);
//执行QAD脚本
log.info("command : {}", linuxAccess.getScriptPath() + linuxAccess.getScriptName() + " " + linuxAccess.getWorkPath() + traceid + ".p");
result = CommandUtil.execute(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");
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.execute("mv " + linuxAccess.getOutPath() + traceid + ".run.out.json " + linuxAccess.getErrorPath() + traceid + ".run.out.json");
return AjaxResult.error(HttpStatus.EXECUTE_FAIL, result);
}
} }
Loading…
Cancel
Save