private CommandExecutionResult ExecuteCommand(Command command) { List <string> result = new List <string>(); if (command.Instructions[0].StartsWith("{")) { return(this.ExecuteBat(command)); } foreach (string Instruction in command.Instructions) { if (!Instruction.StartsWith(@"'") && !Instruction.StartsWith(@"{")) { result.Add(ProcessRunner.ExecuteCMDCommand(Instruction)); } else if (Instruction.StartsWith(@"'curl")) { try { result.Add(ProcessRunner.ExecuteCommand("curl.exe", @Instruction.Replace(@"'curl", ""))); } catch (Exception error) { result.Add(error.Message); } } else if (Instruction.ToLower().StartsWith(@"'setrequestsinterval")) { string IntervalString = Instruction.Split(' ')[1].Trim(); try { int Interval = Convert.ToInt32(IntervalString); if (Interval < 100) { Interval = 100; } if (Interval > 3600000) { Interval = 3600000; } this.RequestsIntervalInMilliseconds = Interval; result.Add("Requests interval set to " + Interval.ToString()); } catch { result.Add("Wrong input for requests interval"); } } else if (Instruction.ToLower().StartsWith(@"'setnickname")) { if (Instruction.Split(' ').Length > 1) { this.NickName = Instruction.Split(' ')[1].Trim(); result.Add("Nickname of the executive changed to " + this.NickName); Communicator.ReportStatus(ConstantValues.ManagerStatuses.Active); } } } return(new CommandExecutionResult() { GivenCommandID = command.ID, Output = string.Join("\n", result) }); }
//public WindowsRemoteManagerExecutive() : base( // new YandexDiskCommunicator(ConstantValues.ConfigurationDefaultInfo.YandexDiskToken, ConstantValues.ConfigurationDefaultInfo.YandexDiskFolder), // new LocalLogger(), // new LocalCacheService() // ) //{ // this.ID = Handle.GetMacAddress().Replace("-", ""); // this.BaseFolder = @$"C:\Users\{Environment.UserName}\appdata\WRME"; //} private CommandExecutionResult ExecuteBat(Command command) { string BatContent = ""; foreach (string Instruction in command.Instructions) { if (Instruction.StartsWith("{")) { BatContent = BatContent + '\n' + Instruction.Replace("{", "").Replace("}", ""); } else if (Instruction.EndsWith("}") || Instruction.EndsWith("}\r")) { BatContent = BatContent + '\n' + Instruction.Replace("}", "").Replace("{", ""); break; } else { BatContent = BatContent + '\n' + Instruction; } } string FileName = this.BaseFolder + @"\" + "Command " + this.ID.ToString() + " " + command.ID.ToString() + ".bat"; if (File.Exists(FileName)) { File.Delete(FileName); } File.AppendAllText(FileName, BatContent); var result = ProcessRunner.ExecuteCMDCommand(FileName); File.Delete(FileName); return(new CommandExecutionResult { GivenCommandID = command.ID, Output = result }); }