xref: /openbmc/openbmc-test-automation/ffdc/plugins/ssh_execution.py (revision 0cfbd5c1ebbd535e7a4f824107a68e1b57d1262d)
1e7e9171eSGeorge Keishing#!/usr/bin/env python3
22f3a96edSPeter D  Phan
32f3a96edSPeter D  Phan
42f3a96edSPeter D  Phanimport os
52f3a96edSPeter D  Phanimport sys
62f3a96edSPeter D  Phan
72f3a96edSPeter D  Phan# ---------Set sys.path for pluqin execution---------------------------------------
82f3a96edSPeter D  Phan# Absolute path to this plugin
92f3a96edSPeter D  Phanabs_path = os.path.abspath(os.path.dirname(sys.argv[0]))
102f3a96edSPeter D  Phan# full_path to plugins parent directory
1120f38712SPatrick Williamsfull_path = abs_path.split("plugins")[0]
122f3a96edSPeter D  Phansys.path.append(full_path)
132f3a96edSPeter D  Phan# Walk path and append to sys.path
142f3a96edSPeter D  Phanfor root, dirs, files in os.walk(full_path):
152f3a96edSPeter D  Phan    for found_dir in dirs:
162f3a96edSPeter D  Phan        sys.path.append(os.path.join(root, found_dir))
172f3a96edSPeter D  Phan
182f3a96edSPeter D  Phan# ssh_utility is in ../lib
1909679890SGeorge Keishingfrom ssh_utility import SSHRemoteclient  # NOQA
202f3a96edSPeter D  Phan
212f3a96edSPeter D  Phan
2220f38712SPatrick Williamsdef ssh_execute_cmd(
23*0cfbd5c1SGeorge Keishing    hostname, username, password, port_ssh, command, timeout=60, type=None
2420f38712SPatrick Williams):
252f3a96edSPeter D  Phan    r"""
262f3a96edSPeter D  Phan    Description of argument(s):
272f3a96edSPeter D  Phan
282f3a96edSPeter D  Phan    hostname        Name/IP of the remote (targeting) host
292f3a96edSPeter D  Phan    username        User on the remote host with access to FFCD files
302f3a96edSPeter D  Phan    password        Password for user on remote host
31*0cfbd5c1SGeorge Keishing    port_ssh        SSH port value. By default 22.
322f3a96edSPeter D  Phan    command         Command to run on remote host
332f3a96edSPeter D  Phan    timeout         Time, in second, to wait for command completion
346ee86702SGeorge Keishing    type            Data type return as list or others.
352f3a96edSPeter D  Phan    """
36*0cfbd5c1SGeorge Keishing    ssh_remoteclient = SSHRemoteclient(hostname, username, password, port_ssh)
372f3a96edSPeter D  Phan
382f3a96edSPeter D  Phan    cmd_exit_code = 0
3920f38712SPatrick Williams    err = ""
4020f38712SPatrick Williams    response = ""
412f3a96edSPeter D  Phan    if ssh_remoteclient.ssh_remoteclient_login():
422f3a96edSPeter D  Phan        """
432f3a96edSPeter D  Phan        cmd_exit_code: command exit status from remote host
442f3a96edSPeter D  Phan        err: stderr from remote host
452f3a96edSPeter D  Phan        response: stdout from remote host
462f3a96edSPeter D  Phan        """
4720f38712SPatrick Williams        cmd_exit_code, err, response = ssh_remoteclient.execute_command(
4820f38712SPatrick Williams            command, int(timeout)
4920f38712SPatrick Williams        )
502f3a96edSPeter D  Phan
512f3a96edSPeter D  Phan    # Close ssh session
522f3a96edSPeter D  Phan    if ssh_remoteclient:
532f3a96edSPeter D  Phan        ssh_remoteclient.ssh_remoteclient_disconnect()
542f3a96edSPeter D  Phan
556ee86702SGeorge Keishing    if type == "list":
5620f38712SPatrick Williams        return response.split("\n")
576ee86702SGeorge Keishing    else:
582f3a96edSPeter D  Phan        return response
59