From dc1a48ed43e65f1f4e62242857cf43d1cf04143f Mon Sep 17 00:00:00 2001 From: root <295172551@qq.com> Date: Fri, 13 Jun 2025 23:25:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=A7=E8=A1=8C=E5=91=BD=E4=BB=A4=E5=8A=A0?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E6=97=B6=E9=97=B4=20=E5=92=8C=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=20=E8=BD=AC=E5=AE=9A=E4=B9=89=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Command/Executors/LinuxCommandExecutor.cs | 5 ++--- .../Services/Network/CellularNetworkService.cs | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/CoreAgent.Infrastructure/Command/Executors/LinuxCommandExecutor.cs b/CoreAgent.Infrastructure/Command/Executors/LinuxCommandExecutor.cs index 70f6c77..54576be 100644 --- a/CoreAgent.Infrastructure/Command/Executors/LinuxCommandExecutor.cs +++ b/CoreAgent.Infrastructure/Command/Executors/LinuxCommandExecutor.cs @@ -91,14 +91,13 @@ public class LinuxCommandExecutor : BaseCommandExecutor throw new ArgumentException("命令不能为空", nameof(command)); } - // 转义命令中的特殊字符 + // 使用双引号包裹整个命令,并对特殊字符进行转义 var escapedCommand = command .Replace("\"", "\\\"") - .Replace("'", "\\'") .Replace("$", "\\$") .Replace("`", "\\`") .Replace("\\", "\\\\"); - return $"-c '{escapedCommand}'"; + return $"-c \"{escapedCommand}\""; } } \ No newline at end of file diff --git a/CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs b/CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs index fc91ef7..8697be9 100644 --- a/CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs +++ b/CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs @@ -187,10 +187,10 @@ public class CellularNetworkService : ICellularNetworkService { if (command.CanBeKilled) { - await KillProcessAsync(command.Template); + await KillProcessAsync(command.Template, command.Timeout); } - var result = await _commandExecutor.ExecuteCommandAsync(command.Template, _context.token); + var result = await _commandExecutor.ExecuteCommandAsync(command.Template, new CancellationTokenSource(),command.Timeout); return result.IsSuccess; } catch (Exception ex) @@ -200,7 +200,7 @@ public class CellularNetworkService : ICellularNetworkService } } - private async Task KillProcessAsync(string template) + private async Task KillProcessAsync(string template,int Timeout) { string killCmd = $"ps -ef | grep {template} | grep -v grep | awk '{{print $2}}' | xargs kill -9"; await _commandExecutor.ExecuteCommandAsync(killCmd, new CancellationTokenSource());