简体   繁体   English

如何在python telnetlib中禁用telnet回声?

[英]How to disable telnet echo in python telnetlib?

Hi I known that I should send 'IAC DONT ECHO' message, but how may I do that using telnetlib?? 嗨,我知道我应该发送“ IAC DONT ECHO”消息,但是如何使用telnetlib呢? Here is my test, but it doesn't work. 这是我的测试,但是不起作用。

 #!/usr/bin/env python2
 u"""test"""
 # -*- coding: utf-8 -*-

 import sys
 import telnetlib
 import time

 HOST = "10.10.5.1"
 tn = telnetlib.Telnet(HOST, timeout=1)
 tn.read_until("login: ")
 tn.write("login\n")
 tn.read_until("Password: ")
 tn.write("pass\n")

 print "###########"
 time.sleep(0.5)
 print tn.read_very_eager()

 tn.write("ls /\n")
 time.sleep(0.5)
 print tn.read_very_eager()

 # diable echo here
 tn.write(telnetlib.IAC + "\n")
 tn.write(telnetlib.DONT + " " + telnetlib.ECHO + "\n")
 time.sleep(0.5)
 print tn.read_very_eager()

 tn.write("ls /\n")
 time.sleep(0.5)
 print tn.read_very_eager()

 print "########### exit"
 tn.write("exit\n")
 print tn.read_all()

You are sending the sequence wrong: 您发送的序列错误:

# diable echo here
tn.write(telnetlib.IAC + "\n")
tn.write(telnetlib.DONT + " " + telnetlib.ECHO + "\n")

THE IAC DONT ECHO is sent as three bytes, without any padding, spaces or newlines. IAC DONT ECHO以三个字节发送,没有任何填充,空格或换行符。 So try this instead: 因此,请尝试以下操作:

tn.write(telnetlib.IAC + telnetlib.DONT + telnetlib.ECHO)

However, it might not be enough to turn off echo actually. 但是,实际上关闭回声可能还不够。 The solution most commonly used is actually to say that you will do the echoing, which will make the other end stop doing echoing: 实际上,最常用的解决方案是说将执行回显,这将使另一端停止执行回显:

tn.write(telnetlib.IAC + telnetlib.WILL + telnetlib.ECHO)

Edit: After reading the telnetlib manual page I see that the write function will: 编辑:阅读telnetlib手册页后,我看到write功能将:

Write a string to the socket, doubling any IAC characters. 将一个字符串写入套接字,将所有IAC字符加倍。

So using the Telnet object write function will not work sending these sequences, you have to get the socket and use that to write the sequence: 因此,使用Telnet对象write功能将无法发送这些序列,您必须获取套接字并使用它来写入序列:

def write_raw_sequence(tn, seq):
    sock = tn.get_socket()
    if sock is not None:
        sock.send(seq)

write_raw_sequence(tn, telnetlib.IAC + telnetlib.WILL + telnetlib.ECHO)

It is impossible to disable telnet echo using telnetlib.py in it's current state. 在当前状态下无法使用telnetlib.py禁用telnet回显。

telnetlib.py automatically responds to IAC commands for you. telnetlib.py会自动为您响应IAC命令。 (See telnetlib.process_rawq()) Unfortunately, it is a simple implementation that just negatively responds to incoming requests. (请参阅telnetlib.process_rawq())不幸的是,这是一个简单的实现,只是对传入的请求做出负面响应。 When you connect, the remote end will send IAC WILL ECHO and your end will reply with IAC DONT ECHO. 当您连接时,远端将发送IAC WILL ECHO,而您的一端将以IAC DONT ECHO进行回复。 (You can confirm this by setting DEBUGLEVEL to 1 in telnetlib.py and running your code). (您可以通过在telnetlib.py中将DEBUGLEVEL设置为1并运行代码来确认这一点)。 The issue is likely to be that other commands are not being handled correctly. 问题可能是其他命令未正确处理。

You can modify telnetlib.py so as to have full control over the control responses by following the instructions below. 您可以按照以下说明修改telnetlib.py,以完全控制控制响应。 However, correctly responding requires sufficient knowledge of the telnet protocol. 但是,正确响应需要对telnet协议有足够的了解。

You will have to disable the automatic responses by setting line 440 in telnetlib.py from 您必须通过从以下位置设置telnetlib.py中的第440行来禁用自动响应:

if c != IAC:

to

if c:

Then disable the doubling of IAC symbol by commenting out line 289-290 然后通过注释掉第289-290行来禁用IAC符号的加倍

if IAC in buffer:
    buffer = buffer.replace(IAC, IAC+IAC)

With that done, the following example of how to send IAC WILL ECHO will work 完成此操作后,以下有关如何发送IAC WILL ECHO的示例将起作用

tn.write(telnetlib.IAC + telnetlib.WILL + telnetlib.ECHO)

It might be that the telnet-server just ignores your telnet-requests as is with the one I was trying against. 可能是telnet服务器只是忽略了您的telnet请求,就像我尝试的那样。 There is no need however to actually edit the telnetlib-code, at least if the telnet-server does send initial IAC-commands on it's own. 但是,至少在telnet服务器确实自己发送初始IAC命令的情况下,实际上不需要编辑telnetlib代码。 At least in python 3.5 telnetlib offers to place a callback to handle IAC-commands. 至少在python 3.5中,telnetlib提供了放置回调以处理IAC命令的功能。 See Stackoverflow answer on negotiating telnet line length . 有关协商telnet线长的信息,请参见Stackoverflow答案 Ie: to install a callback that just does the same as telnetlib does already just do: 即:要安装一个与telnetlib相同的回调,只需执行以下操作即可:

import telnetlib
from telnetlib import DO, DONT, IAC, WILL, WONT


def telnet_option_negotiation_cb(tsocket, command, option):
    """
    :param tsocket: telnet socket object
    :param command: telnet Command
    :param option: telnet option
    :return: None
    """
    if command in (DO, DONT):
        print("CB-send: IAC WONT " + str(ord(option)))
        tsocket.sendall(IAC + WONT + option)
    elif command in (WILL, WONT):
        print("CB-send: IAC DONT " + str(ord(option)))
        tsocket.sendall(IAC + DONT + option)

telnetlib.DEBUGLEVEL = 1
tn = telnetlib.Telnet("192.your.ip.here")
tn.set_option_negotiation_callback(telnet_option_negotiation_cb)

...

The code sets the telnetlib debuglevel and prints the callback-answers, this give you a good output of your conversion with the telnet-server. 该代码设置telnetlib调试级别并打印回调答案,这将为您提供与telnet服务器的转换的良好输出。 Now you can change your answers ie for ECHO by checking the given option in the telnet_option_negotiation_cb() and providing custom answers instead of the standard ones. 现在,您可以通过检查telnet_option_negotiation_cb()中的给定选项并提供自定义答案(而不是标准答案)来更改您的答案,例如针对ECHO。 Ie: 即:

if option == telnetlib.ECHO:
    print("CB-send: IAC WILL ECHO")
    tsocket.sendall(IAC + WILL + option)
    print("CB-send: IAC DONT ECHO")
    tsocket.sendall(IAC + DONT + option)

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

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