I have been going crazy trying to make this work. I want to collect a telnet log from python. On plain telnet I would just open telnet and use:
set logfile test.xml
The problem is that with python I cannot figure out how to pass this command first. when I run this
try:
tn = telnetlib.Telnet()
tn.write(b"set logfile test.xml" + b"\r")
tn.open(IP, 223, 8192)
except socket.timeout:
print ("socket timeout")
sulk()
I get this error:
Traceback (most recent call last):
File "C:xxxxxxxx.py", line 18, in <module>
tn.write(b"set logfile test.xml" + b"\r")
File "C:\Python33\lib\telnetlib.py", line 282, in write
self.sock.sendall(buffer)
AttributeError: 'NoneType' object has no attribute 'sendall'
The script runs fine if I open the IP/port without setting the log file. And to solve the issue I write to a file using python. but for some reason this takes a very long time 5+ minutes. If I just run the commands through telnet it takes under 10 seconds…
telnet
set logfile test.xml
o xxx.xxx.xxx.xxx xxx
>>commandx
>>commandy
All the data I need is here and in the log file
So is there anything I can do to set the logfile before opening the IP/port?
Thanks in Advance!
You’re going about this backward. I assume you’re trying to log it to a file so that you can then read it into your Python script and do something with it. What you should be doing is capturing the the output from the telnet session in a variable. Then you can process it further, even write it to a file if you want to, and so on.
If the output could be large, you can read it in chunks: