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

@@ -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);
}
}