1#!/usr/bin/env python3
2
3
4from ssh_utility import SSHRemoteclient
5
6import os
7import sys
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(hostname,
24                    username,
25                    password,
26                    command,
27                    timeout=60,
28                    type=None):
29    r"""
30        Description of argument(s):
31
32        hostname        Name/IP of the remote (targeting) host
33        username        User on the remote host with access to FFCD files
34        password        Password for user on remote host
35        command         Command to run on remote host
36        timeout         Time, in second, to wait for command completion
37        type            Data type return as list or others.
38    """
39    ssh_remoteclient = SSHRemoteclient(hostname,
40                                       username,
41                                       password)
42
43    cmd_exit_code = 0
44    err = ''
45    response = ''
46    if ssh_remoteclient.ssh_remoteclient_login():
47
48        """
49        cmd_exit_code: command exit status from remote host
50        err: stderr from remote host
51        response: stdout from remote host
52        """
53        cmd_exit_code, err, response = \
54            ssh_remoteclient.execute_command(command, int(timeout))
55
56    # Close ssh session
57    if ssh_remoteclient:
58        ssh_remoteclient.ssh_remoteclient_disconnect()
59
60    if type == "list":
61        return response.split('\n')
62    else:
63        return response
64