如何在python telnetlib中禁用telnet回声? [英] How to disable telnet echo in python telnetlib?

查看:132
本文介绍了如何在python telnetlib中禁用telnet回声?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我应该发送 IAC DONT ECHO消息,但是我该如何使用telnetlib呢?
这是我的测试,但不起作用。

Hi I known that I should send 'IAC DONT ECHO' message, but how may I do that using 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")

IAC DONT ECHO 发送为三个字节,没有任何填充,空格或换行符。因此,请尝试以下操作:

THE IAC DONT ECHO is sent as three bytes, without any padding, spaces or newlines. So try this instead:

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

但是,实际上关闭回声可能还不够。实际上,最常用的解决方案是说 you 将执行回显,这将使另一端停止进行回显:

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)

编辑:阅读了 telnetlib手册页后,我看到了 write 函数将:

After reading the telnetlib manual page I see that the write function will:


将一个字符串写入套接字,将任何字符串加倍IAC字符。

Write a string to the socket, doubling any IAC characters.

因此,使用Telnet对象 write 函数将无法发送这些序列,您必须获取套接字并使用它来编写序列:

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:

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)

这篇关于如何在python telnetlib中禁用telnet回声?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆