I have made some code that will login to a device and send all your additional commands. It also logs the commands and the returned values to a file on the local system. Hope somebody can tell me if the code is a little effective or how I can optimize it. I also have a question about the Sleep I think it's not so effective because some commands take longer then others. How can I resolve this can you let it sleep until a certain reply is given?As I run the code with a other program which not give feedback other then failed. That is why the errors are redirected to a file.#Modules imported when white they are used in the script#import sys, traceback, telnetlib, time## When Using main function tracebacks (errors)# Are not shown in the terminal/console but are logged to.# C:/temp/errlog.txt also see bottom of script for code usage.##TO RUN IT FROM CMD#python Telnet_Program.py 192.168.10.2 23 cisco cisco enable cisco conf t hostname R5 end wr mem  show users show ip int brief#path_of_script = sys.argv[0]host_address = sys.argv[1]port = sys.argv[2]termlen = sys.argv.insert(7, term len 0)##Define globalsglobal newfilesglobal tn##open file for output. first open/close to empty file.newfiles = open('C:/temp/my-test.txt', 'w')newfiles.close()newfiles = open('C:/temp/my-test.txt', 'a')## #creaete new var for Telnet session.tn = telnetlib.Telnet()#tn.set_debuglevel(1)def main():    def telnet_Program(host_address, port):                Function for login and parsing commands to telnet session.        Which utilize read_data_tn_session and write_data_to_file functions.        The read_data_tn function will empty the buffer into a list which is writen to a file by the        write_data_to_file function.                tn.open(str(host_address), int(port), 5)        #Pars extra commands given through the Telnet interperter.        #for x in args() use all extra values right of path/user/password:        for x in sys.argv[3:]:            #Sleep to receive input from Telnet session before reading            #write_data_to_file()            #Write variables to node. and wait before reading.            tn.write(x.encode(ascii)+b\r\n)            write_data_to_file()        #read last command and close connection to host.        write_data_to_file()        #pri_prefix_nr = result_pri_prefix_service_analyzelen[(result_pri_prefix_service_analyze)-2].split(' ')        newfiles.close()        tn.close()#Call the def telnet_program)    telnet_Program(host_address, port)#while data in the loop read the output and write to file.def read_data_tn_session():        While data in the read buffer loop and add to list lines_return. Then return the complete buffer as a list.    In this case return the list to the write_data function.        while True:        #Get the information from the buffer and split it on \r\n.        line = tn.read_very_eager().decode().split(\r\n)        #print(line)        #Create empty variable for usage in for x in line loop.        lines_return = []        for x in line:            #check if line received is not empty. Then add it to the lines_return list.            if x:                lines_return.append(x)        return lines_returndef write_data_to_file():        If data in read/buffer from telnet session call read_data_tn_session.    This will create a list which we then write to an file.        #    #wait until receive buffer is filled with telnet information.    time.sleep(0.5)    #    #use read_data loop to read the data from telnet session and create a list from it.    lines = read_data_tn_session()    #    #print(lines)    #    if lines:        #newfiles.write(lines.pop() + '\n')        for x in lines:            newfiles.write(x + '\n')!!!!!!!!!!!!PART OF MAIN Def!!!!!!!!!!!!!!!!!Let you log all errors generated by functions!Within the main function.!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!if __name__==__main__:    try:        main()    except:        print (Trigger Exception, traceback info forward to log file.)        traceback.print_exc(file=open(C:/temp/errlog.txt,w))        sys.exit(1)
Execute commands over Telnet, logging to a file
python;python 3.x;console;networking