windows 使用脚本执行命令.

This commit is contained in:
lijiahang
2024-04-19 17:31:24 +08:00
parent c880065e39
commit 38f64eb3bb
6 changed files with 38 additions and 21 deletions

View File

@@ -19,25 +19,34 @@ public class PathUtils {
/**
* 获取用户根目录
*
* @param username 用户名
* @param isWindows isWindows
* @param username 用户名
* @return 用户目录
*/
public static String getHomePath(String username) {
if (Const.ROOT.equals(username)) {
return "/" + Const.ROOT;
public static String getHomePath(boolean isWindows, String username) {
if (isWindows) {
// windows
return "C:/Users/" + username;
} else {
return "/home/" + username;
// linux
if (Const.ROOT.equals(username)) {
return "/" + Const.ROOT;
} else {
return "/home/" + username;
}
}
}
/**
* 获取应用路径
*
* @param username username
* @param isWindows isWindows
* @param username username
* @return path
*/
public static String getAppPath(String username) {
return getHomePath(username)
public static String getAppPath(boolean isWindows, String username) {
return getHomePath(isWindows, username)
+ "/" + AppConst.ORION
+ "/" + AppConst.ORION_OPS_PRO;
}
@@ -45,12 +54,13 @@ public class PathUtils {
/**
* 构建应用路径
*
* @param username username
* @param paths paths
* @param isWindows isWindows,
* @param username username
* @param paths paths
* @return path
*/
public static String buildAppPath(String username, Object... paths) {
StringBuilder path = new StringBuilder(getAppPath(username));
public static String buildAppPath(boolean isWindows, String username, Object... paths) {
StringBuilder path = new StringBuilder(getAppPath(isWindows, username));
for (Object o : paths) {
path.append("/").append(Objects1.toString(o));
}

View File

@@ -84,8 +84,7 @@ spring:
tablePrefix: QRTZ_
misfireThreshold: 60000
clusterCheckinInterval: 5000
# 打开群集功能
isClustered: false
isClustered: true
#连接池
threadPool:
class: org.quartz.simpl.SimpleThreadPool

View File

@@ -146,11 +146,18 @@ public class ExecCommandHandler implements IExecCommandHandler {
// 打开 sftp
sftpExecutor = sessionStore.getSftpExecutor(execHostCommand.getFileNameCharset());
sftpExecutor.connect();
// 上传文件
// 必须要以 / 开头
String scriptPath = execHostCommand.getScriptPath();
if (!scriptPath.startsWith("/")) {
scriptPath = "/" + scriptPath;
}
// 创建文件
sftpExecutor.touch(scriptPath);
// 写入命令
byte[] command = Strings.replaceCRLF(execHostCommand.getCommand()).getBytes(execHostCommand.getFileContentCharset());
sftpExecutor.write(execHostCommand.getScriptPath(), command);
sftpExecutor.write(scriptPath, command);
// 修改权限
sftpExecutor.changeMode(execHostCommand.getScriptPath(), 777);
sftpExecutor.changeMode(scriptPath, 777);
} catch (Exception e) {
throw Exceptions.sftp(e);
} finally {

View File

@@ -56,7 +56,7 @@ public class TransferMessageDispatcher extends AbstractWebSocketHandler {
String id = session.getId();
log.info("TransferMessageHandler-afterConnectionClosed id: {}, code: {}, reason: {}", id, status.getCode(), status.getReason());
// 关闭会话
Streams.close(handlers.get(id));
Streams.close(handlers.remove(id));
}
}

View File

@@ -349,11 +349,12 @@ public class ExecCommandServiceImpl implements ExecCommandService {
* @return scriptPath
*/
private String buildScriptPath(String username, String osType, Long logId, Long hostId) {
HostSshOsTypeEnum os = HostSshOsTypeEnum.of(osType);
String name = PathConst.EXEC
+ "_" + logId
+ "_" + hostId
+ HostSshOsTypeEnum.of(osType).getScriptSuffix();
return PathUtils.buildAppPath(username, PathConst.SCRIPT, name);
+ os.getScriptSuffix();
return PathUtils.buildAppPath(HostSshOsTypeEnum.WINDOWS.equals(os), username, PathConst.SCRIPT, name);
}
}

View File

@@ -1,6 +1,6 @@
<template>
<a-drawer v-model:visible="visible"
:width="420"
:width="460"
:esc-to-close="false"
:mask-closable="false"
:unmount-on-close="true"