简体   繁体   English

Python 3.3 telnet问题

[英]Python 3.3 telnet issue

I have been going crazy trying to make this work. 我一直为使这项工作疯狂。 I want to collect a telnet log from python. 我想从python收集telnet日志。 On plain telnet I would just open telnet and use: 在普通的telnet上,我只需打开telnet并使用:

set logfile test.xml

The problem is that with python I cannot figure out how to pass this command first. 问题是使用python我无法弄清楚如何首先传递此命令。 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. 如果我打开IP /端口而不设置日志文件,则脚本运行良好。 And to solve the issue I write to a file using python. 为了解决这个问题,我使用python写入文件。 but for some reason this takes a very long time 5+ minutes. 但是由于某种原因,这需要5分钟以上的时间。 If I just run the commands through telnet it takes under 10 seconds... 如果我只是通过telnet运行命令,则需要不到10秒的时间...

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? 那么在打开IP /端口之前我可以做些什么来设置日志文件吗? 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. 我假设你想记录到一个文件,这样你就可以阅读到您的Python脚本,并用它做什么。 What you should be doing is capturing the the output from the telnet session in a variable. 你应该做的是捕获来自telnet会话的输出中的一个变量。 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()

Nothing. 没有。

set logfile is not part of the telnet protocol, it's a command for your favourite telnet client. set logfile不是telnet协议的一部分,它是您最喜欢的telnet客户端的命令。 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). 现在,您正在使用telnet客户端的另一个实现 ,该实现没有这样的命令(没有理由尝试将其发送到远程系统)。

As far as I know, there is no built-in logging facility in python's telnetlib to recommend as a replacement. 据我所知,在python的telnetlib没有内置的日志记录工具可以推荐作为替代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM