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