[Tutor] How to save telnet output logs to a text file in python

kailash ganesh kailashgkg at gmail.com
Thu Feb 26 07:54:17 CET 2015


Hi All,

Please someone help on the below request.

I am learning python and trying to connect my satellite decoder using
telnet. When I tried to connect my device through telnet python script, I
could successfully connected to the device.

But i want to capture/save as well as read the output and logs of device
(connected thru telnet) from starting in a text file. Also, i want to send
few commands to get the output of the command and store in the text file.

Please kindly help on how to capture and write the output logs of device
connected thru telnet connection in a local text file.

Note: Once the password is successful, my device prints some set of logs
and it has to capture in the local text file, also the output of commands
should also record in the text file.


   1. HOST = "192.131.244.100"
   2. user = "user"
   3. password = "user"
   4.
   5. tn = Telnet(HOST)
   6. tn.read_until("Login: ")
   7. tn.write(user + "\n")
   8. tn.read_until("Password: ")
   9. tn.write(password + "\n")
   10. time.sleep(5)
   11. tn.write("lr\n") # lr is command to pause logs
   12. tn.write("version\n")  # command to check running software version
   13. tn.write("exit\n")
   14. str_all = tn.read_all()
   15.
   16. f = open("C:\\temp\\Logs\\output.txt", 'w')
   17. f.write(str_all)
   18. tn.close()

Not able to read and save all logs from starting. Also, pls. let me know
any other way to save telnet output either in putty or teraterm.


More information about the Tutor mailing list