xref: /openbmc/openbmc-test-automation/ffdc/plugins/scp_execution.py (revision 7bc5ce3a40b19f552580c5a2712cad141a28c9fc)
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 scp_remote_file(
23    hostname, username, password, port_ssh, filename, local_dir_path
24):
25    r"""
26    Copy a file from a remote host to the local host using SCP.
27
28    This function copies a file from a remote host to the local host using the
29    SCP protocol. The function takes the remote host details (hostname,
30    username, password), the filename with its full path on the remote host,
31    and the local directory path as arguments.
32
33    The function uses wildcards to support copying multiple files if needed.
34
35    Parameters:
36        hostname (str):       Name or IP address of the remote host.
37        username (str):       User on the remote host with access to files.
38        password (str):       Password for the user on the remote host.
39        port_ssh (int):       SSH/SCP port value. By default, 22.
40        filename (str):       Filename with full path on the remote host.
41                              Can contain wildcards for multiple files.
42        local_dir_path (str): Location to store the file on the local host.
43
44    Returns:
45        None
46    """
47    ssh_remoteclient = SSHRemoteclient(hostname, username, password, port_ssh)
48
49    if ssh_remoteclient.ssh_remoteclient_login():
50        # Obtain scp connection.
51        ssh_remoteclient.scp_connection()
52        if ssh_remoteclient.scpclient:
53            if isinstance(filename, list):
54                for each_file in filename:
55                    ssh_remoteclient.scp_file_from_remote(
56                        each_file, local_dir_path
57                    )
58            else:
59                ssh_remoteclient.scp_file_from_remote(filename, local_dir_path)
60
61    # Close ssh/scp session
62    if ssh_remoteclient:
63        ssh_remoteclient.ssh_remoteclient_disconnect()
64