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 | |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 7 | # ---------Set sys.path for pluqin execution---------------------------------- |
Peter D Phan | 2f3a96e | 2021-07-29 12:12:02 -0500 | [diff] [blame] | 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 |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 11 | full_path = abs_path.split("plugins")[0] |
Peter D Phan | 2f3a96e | 2021-07-29 12:12:02 -0500 | [diff] [blame] | 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 | |
George Keishing | 0967989 | 2022-12-08 08:21:52 -0600 | [diff] [blame] | 18 | from telnet_utility import TelnetRemoteclient # NOQA |
George Keishing | 37c58c8 | 2022-12-08 07:42:54 -0600 | [diff] [blame] | 19 | |
Peter D Phan | 2f3a96e | 2021-07-29 12:12:02 -0500 | [diff] [blame] | 20 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 21 | def telnet_execute_cmd(hostname, username, password, command, timeout=60): |
Peter D Phan | 2f3a96e | 2021-07-29 12:12:02 -0500 | [diff] [blame] | 22 | r""" |
George Keishing | a64fa5f | 2025-05-17 23:40:30 +0530 | [diff] [blame] | 23 | Execute a command on the remote host using Telnet and return the output. |
Peter D Phan | 2f3a96e | 2021-07-29 12:12:02 -0500 | [diff] [blame] | 24 | |
George Keishing | a64fa5f | 2025-05-17 23:40:30 +0530 | [diff] [blame] | 25 | This function executes a provided command on the remote host using Telnet. |
| 26 | The function takes the remote host details (hostname, username, password) |
| 27 | and the command to be executed as arguments. |
| 28 | |
| 29 | The function also accepts an optional timeout parameter, which specifies |
| 30 | the time in seconds to wait for the command to complete. |
| 31 | |
| 32 | The function returns the output of the executed command as a string. |
| 33 | |
| 34 | Parameters: |
| 35 | hostname (str): Name or IP address of the remote host. |
| 36 | username (str): User on the remote host with access to files. |
| 37 | password (str): Password for the user on the remote host. |
| 38 | command (str): The command to be executed on the remote host. |
| 39 | timeout (int, optional): The time in seconds to wait for the command |
| 40 | to complete. Defaults to 60 seconds. |
| 41 | |
| 42 | Returns: |
| 43 | str: The output of the executed command as a string. |
Peter D Phan | 2f3a96e | 2021-07-29 12:12:02 -0500 | [diff] [blame] | 44 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 45 | telnet_remoteclient = TelnetRemoteclient(hostname, username, password) |
| 46 | result = "" |
Peter D Phan | 2f3a96e | 2021-07-29 12:12:02 -0500 | [diff] [blame] | 47 | if telnet_remoteclient.tn_remoteclient_login(): |
| 48 | # result: stdout from remote host |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 49 | result = telnet_remoteclient.execute_command(command, timeout) |
Peter D Phan | 2f3a96e | 2021-07-29 12:12:02 -0500 | [diff] [blame] | 50 | |
| 51 | # Close telnet session |
| 52 | if telnet_remoteclient: |
| 53 | telnet_remoteclient.tn_remoteclient_disconnect() |
| 54 | |
| 55 | return result |