1#!/usr/bin/env python3 2 3 4import os 5import sys 6 7# ---------Set sys.path for pluqin execution---------------------------------- 8# Absolute path to this plugin 9abs_path = os.path.abspath(os.path.dirname(sys.argv[0])) 10# full_path to plugins parent directory 11full_path = abs_path.split("plugins")[0] 12sys.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 18from telnet_utility import TelnetRemoteclient # NOQA 19 20 21def telnet_execute_cmd(hostname, username, password, command, timeout=60): 22 r""" 23 Execute a command on the remote host using Telnet and return the output. 24 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. 44 """ 45 telnet_remoteclient = TelnetRemoteclient(hostname, username, password) 46 result = "" 47 if telnet_remoteclient.tn_remoteclient_login(): 48 # result: stdout from remote host 49 result = telnet_remoteclient.execute_command(command, timeout) 50 51 # Close telnet session 52 if telnet_remoteclient: 53 telnet_remoteclient.tn_remoteclient_disconnect() 54 55 return result 56