(Please note that line should be renamed lines because it is a list of lines not a single line).Īs you can see the core of the logic (discarding blank lines) is still there but without the waste of space and effort that there was before. My favourite feature, it avoids so much boilerplate code: #Create empty variable for usage in for x in line loop. Using w mode clears all content of the file, and you can still write to it, no need to open/close.Īnyway of a comment I would prefer a function erase_file_content for self-documenting code. You can use with to open files to have them close automatically. This is simpler, a, b, c are the first three elements in respective order. If you are not yet ready to refactor like this, then you can use at least tuple unpacking (and slicing): path_of_script, host_address, port = sys.argv Instead of variable length arguments you could make one of them a list and loop through it. Traceback.print_exc(file=open("C:/temp/errlog.txt","w"))Īrgparse / tuple unpacking path_of_script = sys.argvĪrgparse is the standard way of dealing with command line arguments and it automatically generates information messages for the user, so it has extra convenience. Print ("Trigger Exception, traceback info forward to log file.") !Let you log all errors generated by functions #use read_data loop to read the data from telnet session and create a list from it. #wait until receive buffer is filled with telnet information. This will create a list which we then write to an file. If data in read/buffer from telnet session call read_data_tn_session. #Create empty variable for usage in for x in line loop. Line = tn.read_very_eager().decode().split("\r\n") #Get the information from the buffer and split it on \r\n. In this case return the list to the write_data function. Then return the complete buffer as a list. While data in the read buffer loop and add to list lines_return. #while data in the loop read the output and write to file. #pri_prefix_nr = result_pri_prefix_service_analyzelen.split(' ') #read last command and close connection to host. #Sleep to receive input from Telnet session before reading #for x in args() use all extra values right of path/user/password: #Pars extra commands given through the Telnet interperter. The read_data_tn function will empty the buffer into a list which is writen to a file by the Which utilize read_data_tn_session and write_data_to_file functions. Newfiles = open('C:/temp/my-test.txt', 'a')įunction for login and parsing commands to telnet session. Newfiles = open('C:/temp/my-test.txt', 'w') #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" # C:/temp/errlog.txt also see bottom of script for code usage. # Are not shown in the terminal/console but are logged to. # When Using main function tracebacks (errors) #Modules imported when white they are used in the script That is why the errors are redirected to a file. How can I resolve this can you let it sleep until a certain reply is given?Īs I run the code with a other program which not give feedback other then failed. I also have a question about the Sleep I think it's not so effective because some commands take longer then others. Hope somebody can tell me if the code is a little effective or how I can optimize it. It also logs the commands and the returned values to a file on the local system. I have made some code that will login to a device and send all your additional commands.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |