xref: /openbmc/openbmc-test-automation/ffdc/plugins/scp_execution.py (revision 20f38712b324e61a94e174017c487a0af4b373e1)
1e7e9171eSGeorge Keishing#!/usr/bin/env python3
2c892e62bSPeter D  Phan
3c892e62bSPeter D  Phan
4e635ddc0SGeorge Keishingimport os
5e635ddc0SGeorge Keishingimport sys
6e635ddc0SGeorge Keishing
7c892e62bSPeter D  Phan# ---------Set sys.path for pluqin execution---------------------------------------
8c892e62bSPeter D  Phan# Absolute path to this plugin
9c892e62bSPeter D  Phanabs_path = os.path.abspath(os.path.dirname(sys.argv[0]))
10c892e62bSPeter D  Phan# full_path to plugins parent directory
11*20f38712SPatrick Williamsfull_path = abs_path.split("plugins")[0]
12c892e62bSPeter D  Phansys.path.append(full_path)
13c892e62bSPeter D  Phan# Walk path and append to sys.path
14c892e62bSPeter D  Phanfor root, dirs, files in os.walk(full_path):
15c892e62bSPeter D  Phan    for found_dir in dirs:
16c892e62bSPeter D  Phan        sys.path.append(os.path.join(root, found_dir))
17c892e62bSPeter D  Phan
1837c58c8cSGeorge Keishing# ssh_utility is in ../lib
1909679890SGeorge Keishingfrom ssh_utility import SSHRemoteclient  # NOQA
2037c58c8cSGeorge Keishing
21c892e62bSPeter D  Phan
22*20f38712SPatrick Williamsdef scp_remote_file(hostname, username, password, filename, local_dir_path):
23c892e62bSPeter D  Phan    r"""
24c892e62bSPeter D  Phan    Description of argument(s):
25c892e62bSPeter D  Phan
26c892e62bSPeter D  Phan    hostname        Name/IP of the remote (targeting) host
27c892e62bSPeter D  Phan    username        User on the remote host with access to files
28c892e62bSPeter D  Phan    password        Password for user on remote host
29c892e62bSPeter D  Phan    filename        Filename with full path on remote host
30c892e62bSPeter D  Phan                    Filename can contain wild cards for multiple files
31c892e62bSPeter D  Phan    local_dir_path  Location to store file on local host
32c892e62bSPeter D  Phan    """
33*20f38712SPatrick Williams    ssh_remoteclient = SSHRemoteclient(hostname, username, password)
34c892e62bSPeter D  Phan
35c892e62bSPeter D  Phan    if ssh_remoteclient.ssh_remoteclient_login():
36c892e62bSPeter D  Phan        # Obtain scp connection.
37c892e62bSPeter D  Phan        ssh_remoteclient.scp_connection()
38c892e62bSPeter D  Phan        if ssh_remoteclient.scpclient:
39c892e62bSPeter D  Phan            if isinstance(filename, list):
40c892e62bSPeter D  Phan                for each_file in filename:
41*20f38712SPatrick Williams                    ssh_remoteclient.scp_file_from_remote(
42*20f38712SPatrick Williams                        each_file, local_dir_path
43*20f38712SPatrick Williams                    )
44c892e62bSPeter D  Phan            else:
45c892e62bSPeter D  Phan                ssh_remoteclient.scp_file_from_remote(filename, local_dir_path)
46c892e62bSPeter D  Phan
47c892e62bSPeter D  Phan    # Close ssh/scp session
48c892e62bSPeter D  Phan    if ssh_remoteclient:
49c892e62bSPeter D  Phan        ssh_remoteclient.ssh_remoteclient_disconnect()
50