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(hostname, username, password, filename, local_dir_path):
23    r"""
24    Description of argument(s):
25
26    hostname        Name/IP of the remote (targeting) host
27    username        User on the remote host with access to files
28    password        Password for user on remote host
29    filename        Filename with full path on remote host
30                    Filename can contain wild cards for multiple files
31    local_dir_path  Location to store file on local host
32    """
33    ssh_remoteclient = SSHRemoteclient(hostname, username, password)
34
35    if ssh_remoteclient.ssh_remoteclient_login():
36        # Obtain scp connection.
37        ssh_remoteclient.scp_connection()
38        if ssh_remoteclient.scpclient:
39            if isinstance(filename, list):
40                for each_file in filename:
41                    ssh_remoteclient.scp_file_from_remote(
42                        each_file, local_dir_path
43                    )
44            else:
45                ssh_remoteclient.scp_file_from_remote(filename, local_dir_path)
46
47    # Close ssh/scp session
48    if ssh_remoteclient:
49        ssh_remoteclient.ssh_remoteclient_disconnect()
50