xref: /openbmc/openbmc-test-automation/ffdc/plugins/ssh_execution.py (revision e5c4439f14a8fc199a34e9a250a071855c96b1ef)
1#!/usr/bin/env python3
2
3
4import os
5import sys
6
7# ---------Set sys.path for pluqin execution----------------------------------
8# Absolute path to this plugin
9abs_path = os.path.abspath(os.path.dirname(sys.argv[0]))
10# full_path to plugins parent directory
11full_path = abs_path.split("plugins")[0]
12sys.path.append(full_path)
13# Walk path and append to sys.path
14for root, dirs, files in os.walk(full_path):
15    for found_dir in dirs:
16        sys.path.append(os.path.join(root, found_dir))
17
18# ssh_utility is in ../lib
19from ssh_utility import SSHRemoteclient  # NOQA
20
21
22def ssh_execute_cmd(
23    hostname, username, password, port_ssh, command, timeout=60, type=None
24):
25    r"""
26    Execute a command on the remote host using SSH and return the output.
27
28    This function executes a provided command on the remote host using SSH.
29    The function takes the remote host details (hostname, username, password,
30    and SSH port) and the command to be executed as arguments.
31
32    The function also accepts an optional timeout parameter, which specifies
33    the time in seconds to wait for the command to complete.
34
35    The function returns the output of the executed command as a string or
36    list
37
38    Parameters:
39        hostname (str):          Name or IP address of the remote host.
40        username (str):          User on the remote host.
41        password (str):          Password for the user on the remote host.
42        port_ssh (int):          SSH port value. By default, 22.
43        command (str):           The command to be executed on the remote host.
44        timeout (int, optional): The time in seconds to wait for the command
45                                 to complete. Defaults to 60 seconds.
46        type (str, optional):    The data type to return. If set to list,
47                                 the function returns a list of lines from the
48                                 command output. Defaults to None.
49
50    Returns:
51        str or list: The output of the executed command as a string or a list
52                     of lines, depending on the type parameter.
53    """
54    ssh_remoteclient = SSHRemoteclient(hostname, username, password, port_ssh)
55
56    cmd_exit_code = 0
57    err = ""
58    response = ""
59    if ssh_remoteclient.ssh_remoteclient_login():
60        """
61        cmd_exit_code: command exit status from remote host
62        err: stderr from remote host
63        response: stdout from remote host
64        """
65        cmd_exit_code, err, response = ssh_remoteclient.execute_command(
66            command, int(timeout)
67        )
68
69    # Close ssh session
70    if ssh_remoteclient:
71        ssh_remoteclient.ssh_remoteclient_disconnect()
72
73    if type == "list":
74        return response.split("\n")
75    else:
76        return response
77