blob: ca2aabe78220d10c4eb3cc3ac9fc16938dfe3873 [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
Peter D Phan2f3a96e2021-07-29 12:12:02 -05002
3
4import os
5import sys
6
George Keishingc754b432025-04-24 14:27:14 +05307# ---------Set sys.path for pluqin execution----------------------------------
Peter D Phan2f3a96e2021-07-29 12:12:02 -05008# Absolute path to this plugin
9abs_path = os.path.abspath(os.path.dirname(sys.argv[0]))
10# full_path to plugins parent directory
Patrick Williams20f38712022-12-08 06:18:26 -060011full_path = abs_path.split("plugins")[0]
Peter D Phan2f3a96e2021-07-29 12:12:02 -050012sys.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
George Keishing09679892022-12-08 08:21:52 -060018from telnet_utility import TelnetRemoteclient # NOQA
George Keishing37c58c82022-12-08 07:42:54 -060019
Peter D Phan2f3a96e2021-07-29 12:12:02 -050020
Patrick Williams20f38712022-12-08 06:18:26 -060021def telnet_execute_cmd(hostname, username, password, command, timeout=60):
Peter D Phan2f3a96e2021-07-29 12:12:02 -050022 r"""
George Keishinga64fa5f2025-05-17 23:40:30 +053023 Execute a command on the remote host using Telnet and return the output.
Peter D Phan2f3a96e2021-07-29 12:12:02 -050024
George Keishinga64fa5f2025-05-17 23:40:30 +053025 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 Phan2f3a96e2021-07-29 12:12:02 -050044 """
Patrick Williams20f38712022-12-08 06:18:26 -060045 telnet_remoteclient = TelnetRemoteclient(hostname, username, password)
46 result = ""
Peter D Phan2f3a96e2021-07-29 12:12:02 -050047 if telnet_remoteclient.tn_remoteclient_login():
48 # result: stdout from remote host
Patrick Williams20f38712022-12-08 06:18:26 -060049 result = telnet_remoteclient.execute_command(command, timeout)
Peter D Phan2f3a96e2021-07-29 12:12:02 -050050
51 # Close telnet session
52 if telnet_remoteclient:
53 telnet_remoteclient.tn_remoteclient_disconnect()
54
55 return result