xref: /openbmc/openbmc-test-automation/ffdc/plugins/shell_execution.py (revision 069b266e40a93fda3992c888062745b1ac920473)
1#!/usr/bin/env python3
2
3import subprocess
4
5
6def execute_cmd(parms_string, quiet=False):
7    r"""
8    Execute a CLI standard tool or script with the provided command string.
9
10    This function executes a provided command string using the current SHELL.
11    The function takes the parms_string as an argument, which is expected
12    to be a valid command to execute.
13
14    The function also accepts an optional quiet parameter, which, if set to
15    True, suppresses the output of the command.
16
17    The function returns the output of the executed command as a string.
18
19    Parameters:
20        parms_string (str):     The command to execute from the current SHELL.
21        quiet (bool, optional): If True, suppresses the output of the command.
22                                Defaults to False.
23
24    Returns:
25        str: The output of the executed command as a string.
26    """
27    result = subprocess.run(
28        [parms_string],
29        stdout=subprocess.PIPE,
30        stderr=subprocess.PIPE,
31        shell=True,
32        universal_newlines=True,
33    )
34
35    if result.stderr and not quiet:
36        print("\n\t\tERROR with %s " % parms_string)
37        print("\t\t" + result.stderr)
38
39    return result.stdout
40