处理ssh密码中带有特殊字符的情况

This commit is contained in:
even 2025-05-23 11:09:43 +08:00
parent 6e9d521dc5
commit a945af0d35
4 changed files with 11 additions and 5 deletions

View File

@ -60,7 +60,7 @@ public class DIYImageExecuteCommandWorker extends SshWorker {
//获取机器 //获取机器
MachineInfo machineInfoDO = this.getMachineInfoService().getById(machineId); MachineInfo machineInfoDO = this.getMachineInfoService().getById(machineId);
statusCode = shell(machineInfoDO,"1qaz!QAZ",taskLog, statusCode = shell(machineInfoDO,"Hnidc@0626cn!@#zyx",taskLog,
"echo \"自定义镜像执行命令\"", "echo \"自定义镜像执行命令\"",
commandScript commandScript
); );

View File

@ -39,7 +39,7 @@ import java.util.*;
* @Description: * @Description:
*/ */
@Slf4j @Slf4j
@Plugin(taskType = "sbom") @Plugin(taskType = "test")
public class ScaSbomWorker extends HttpWorker{ public class ScaSbomWorker extends HttpWorker{
private static final int POLLING_INTERVAL = 5000; // 轮询间隔单位毫秒 private static final int POLLING_INTERVAL = 5000; // 轮询间隔单位毫秒

View File

@ -9,6 +9,7 @@ import cn.hutool.extra.ssh.JschUtil;
import com.jcraft.jsch.ChannelShell; import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSchException; import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session; import com.jcraft.jsch.Session;
import lombok.extern.slf4j.Slf4j;
import java.io.*; import java.io.*;
import java.util.List; import java.util.List;
@ -21,6 +22,7 @@ import java.util.concurrent.TimeUnit;
* @Date 2022/6/22 15:43 * @Date 2022/6/22 15:43
* @Version 1.0 * @Version 1.0
**/ **/
@Slf4j
public class SshCommand implements SshClient { public class SshCommand implements SshClient {
private static final int BUFF_SIZE = 1024 * 8; // 8KB private static final int BUFF_SIZE = 1024 * 8; // 8KB
@ -180,8 +182,11 @@ public class SshCommand implements SshClient {
String realCmd; String realCmd;
if (sudoPassword != null && !sudoPassword.isEmpty()) { if (sudoPassword != null && !sudoPassword.isEmpty()) {
// 单引号转义防止脚本内容出错 // 单引号转义防止脚本内容出错
String safeCmd = cmd.replace("'", "'\\''"); String safeCmd = cmd.replace("'", "\\'");
realCmd = "echo " + sudoPassword + " | sudo -S bash -c '" + safeCmd + "'"; realCmd = "echo '" + sudoPassword + "' | sudo -S bash -c '" + safeCmd + "'";
log.info("================管理员权限执行命令{}",realCmd);
log.info("================原命令{}",safeCmd);
log.info("");
} else { } else {
realCmd = cmd; realCmd = cmd;
} }

File diff suppressed because one or more lines are too long