mad5245
mad5245

Reputation: 394

Python 3.3 telnet issue

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!

Upvotes: 1

Views: 5574

Answers (2)

kindall
kindall

Reputation: 184455

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.

import telnetlib

host    = "host.example.com"
port    = 223
timeout = 9999

try:
    session = telnetlib.Telnet(host, port, timeout)
except socket.timeout:
    print ("socket timeout")
else:
    session.write("command1\n")
    session.write("command2\n")
    output = session.read_all()
    # output now contains the text you want
    # so now you can do something with it:
    with open("test.xml", "w") as logfile:
        logfile.write(output)

If the output could be large, you can read it in chunks:

import time, telnetlib

host    = "host.example.com"
port    = 223
timeout = 9999

try:
    session = telnetlib.Telnet(host, port, timeout)
except socket.timeout:
    print ("socket timeout")
else:
    session.write("command1\n")
    session.write("command2\n")
    with open("test.xml", "w") as logfile:
        output = session.read_some()
        while output:
            logfile.write(output)
            time.sleep(0.1)  # let the buffer fill up a bit
            output = session.read_some()

Upvotes: 3

Anton Kovalenko
Anton Kovalenko

Reputation: 21517

Nothing.

set logfile is not part of the telnet protocol, it's a command for your favourite telnet client. Now you're working with another implementation of telnet client, which has no such command (there is no reason to try sending it to remote system).

As far as I know, there is no built-in logging facility in python's telnetlib to recommend as a replacement.

Upvotes: 2

Related Questions