TroubleshootExecuteCommand

Executes a command in Cortex XSOAR in debug mode and pulls logs from the command execution.

python · Troubleshoot

Source

"""
Command execute:
    Executes command with debug-mode
"""

from CommonServerPython import *


def _execute_command(command: str, arguments: dict):
    return demisto.executeCommand(command, arguments)


def get_errors(response: Union[list, dict]) -> List[str]:
    errors = []
    if is_error(response):
        if isinstance(response, dict):
            errors.append(response["Contents"])

        for entry in response:
            is_error_entry = type(entry) is dict and entry["Type"] == entryTypes["error"]
            if is_error_entry:
                errors.append(entry["Contents"])
    return errors


def get_log_file(response: Union[list, dict]):
    logs: List[dict]
    if isinstance(response, list):
        logs = [
            {"File": entry["File"], "FileID": entry["FileID"]} for entry in response if entry.get("File", "").endswith(".log")
        ]
    elif response.get("File", "").endswith(".log"):
        logs = [{"File": response["File"], "FileID": response["FileID"]}]
    else:
        raise DemistoException("Could not find the log file")
    return logs


def main():
    args = demisto.args()
    command = args.get("command")
    arguments = args.get("arguments")
    if not arguments:
        arguments = {}
    elif isinstance(arguments, str) and arguments:
        arguments = json.loads(arguments)

    if isinstance(arguments, str):
        arguments = json.loads(arguments)
    instance_name = args.get("instance_name")
    arguments["using"] = instance_name
    arguments["debug-mode"] = True
    response = _execute_command(command, arguments)
    errors = get_errors(response)
    log_files = get_log_file(response)
    for log_file in log_files:
        with open(demisto.getFilePath(log_file["FileID"])["path"]) as stream:
            demisto.results(fileResult(log_file["File"], stream.read()))
    if errors:
        human_readable = tableToMarkdown(f"Errors found for command {command}:\n", errors, ["Errors"])
    else:
        human_readable = f"No errors for command {command}!"
    context = {
        "TroubleshootExecuteCommand(obj.command === val.command && obj.instance_name === val.instance_name)": {
            "command": command,
            "instance_name": instance_name,
            "Error": errors,
        }
    }
    return_outputs(human_readable, context)


if __name__ in ("__main__", "builtin", "builtins"):
    main()

README

Executes a command in Cortex XSOAR in debug mode and pulls logs from the command execution.

Script Data


Name Description
Script Type python3
Tags troubleshoot
Cortex XSOAR Version 5.0.0

Used In


This script is used in the following playbooks and scripts.

  • Integration Troubleshooting

Inputs


Argument Name Description
command The command to run.
arguments The arguments of the command.
instance_name The instance name.

Outputs


Path Description Type
TroubleshootExecuteCommand.command The command executed. String
TroubleshootExecuteCommand.instance_name On which instance of the integration the command executed. String
TroubleshootExecuteCommand.Error The errors from the command. String
File.Name The full file name (including file extension). String
File.EntryID The ID for locating the file in the War Room. String
File.Size The size of the file in bytes. Number
File.MD5 The MD5 hash of the file. String
File.SHA1 The SHA1 hash of the file. String
File.SHA256 The SHA1 hash of the file. String
File.SHA512 The SHA512 hash of the file. String
File.SSDeep The ssdeep hash of the file (same as displayed in file entries). String
File.Extension The file extension, for example: ‘xls’. String
File.Type The file type, as determined by libmagic (same as displayed in the file entries). String