Exemplo n.º 1
0
        /// <summary>
        /// Perform some kind of control action
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public override bool Execute(SshClient client)
        {
            bool success = false;

            try
            {
                Logger.Debug("Command Tx: " + Send);

                var command1 = client.RunCommand(Send);

                // If we have no success response defined but we do have an error response then assume success to begin
                if (!string.IsNullOrEmpty(ErrorResponse) && string.IsNullOrEmpty(SuccessResponse))
                {
                    success = true;
                }

                if (string.IsNullOrEmpty(ErrorResponse) && string.IsNullOrEmpty(SuccessResponse))
                {
                    // Just check exit status
                    if (command1.ExitStatus != 0)
                    {
                        Logger.Warn("Error exit status: " + command1.ExitStatus);
                        if (FailureMode != EnumFailureMode.Warn)
                        {
                            Output = "Error exit status: " + command1.ExitStatus + ": " + command1.Result;
                        }
                        return(false);
                    }
                    success = true;
                }

                var response = command1.Result;

                // For some reason we seem to get command output back in error? (Maybe? It's the error stream - aha...)
                if (string.IsNullOrEmpty(response))
                {
                    response = command1.Error;
                }

                if (!string.IsNullOrEmpty(ErrorResponse))
                {
                    var arrErrorResponses = ErrorResponse.Split('|');
                    foreach (var er in arrErrorResponses)
                    {
                        if (response.Contains(ErrorResponse))
                        {
                            Logger.Warn("- Error response: " + response);
                            return(false);
                        }
                    }
                }

                if (!string.IsNullOrEmpty(SuccessResponse))
                {
                    var arrSuccessResponses = SuccessResponse.Split('|');
                    foreach (var sr in arrSuccessResponses)
                    {
                        if (response.Contains(sr))
                        {
                            Logger.Warn("- Success response: " + response);
                            success = true;
                            break;
                        }
                    }
                }

                // Perform regexp replacement...
                Output = !string.IsNullOrEmpty(OutputRegExp) ? Regex.Match(response, OutputRegExp).Value : response;

                Logger.Debug("Output: " + (!string.IsNullOrEmpty(Output) ? Output : "<none>"));

                // Store output
                if (!string.IsNullOrEmpty(VariableForOutput))
                {
                    Globals.ScriptVariables.Remove(VariableForOutput);
                    Globals.ScriptVariables.Add(VariableForOutput, Output);
                }
            }
            catch (Exception e)
            {
                Logger.Warn("Exception in worker thread: " + e.Message);
                success = false;
            }

            return(success);
        }