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());