1#!/usr/bin/env python3
2
3
4import os
5import sys
6
7from ssh_utility import SSHRemoteclient
8
9# ---------Set sys.path for pluqin execution---------------------------------------
10# Absolute path to this plugin
11abs_path = os.path.abspath(os.path.dirname(sys.argv[0]))
12# full_path to plugins parent directory
13full_path = abs_path.split("plugins")[0]
14sys.path.append(full_path)
15# Walk path and append to sys.path
16for root, dirs, files in os.walk(full_path):
17    for found_dir in dirs:
18        sys.path.append(os.path.join(root, found_dir))
19
20# ssh_utility is in ../lib
21
22
23def ssh_execute_cmd(
24    hostname, username, password, command, timeout=60, type=None
25):
26    r"""
27    Description of argument(s):
28
29    hostname        Name/IP of the remote (targeting) host
30    username        User on the remote host with access to FFCD files
31    password        Password for user on remote host
32    command         Command to run on remote host
33    timeout         Time, in second, to wait for command completion
34    type            Data type return as list or others.
35    """
36    ssh_remoteclient = SSHRemoteclient(hostname, username, password)
37
38    cmd_exit_code = 0
39    err = ""
40    response = ""
41    if ssh_remoteclient.ssh_remoteclient_login():
42        """
43        cmd_exit_code: command exit status from remote host
44        err: stderr from remote host
45        response: stdout from remote host
46        """
47        cmd_exit_code, err, response = ssh_remoteclient.execute_command(
48            command, int(timeout)
49        )
50
51    # Close ssh session
52    if ssh_remoteclient:
53        ssh_remoteclient.ssh_remoteclient_disconnect()
54
55    if type == "list":
56        return response.split("\n")
57    else:
58        return response
59