嗨,我知道我应该发送“ IAC DONT ECHO”消息,但是如何使用telnetlib呢?这是我的测试,但是不起作用。
#!/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()
您发送的序列错误:
# diable echo here tn.write(telnetlib.IAC + "\n") tn.write(telnetlib.DONT + " " + telnetlib.ECHO + "\n")
THEIAC DONT ECHO以三个字节发送,没有任何填充,空格或换行符。因此,请尝试以下操作:
IAC DONT ECHO
tn.write(telnetlib.IAC + telnetlib.DONT + telnetlib.ECHO)
但是,实际上关闭回声可能还不够。实际上,最常用的解决方案是说 您 将执行回显,这将使另一端停止进行回显:
tn.write(telnetlib.IAC + telnetlib.WILL + telnetlib.ECHO)
编辑: 阅读telnetlib手册页后,我看到该write功能将:
write
将一个字符串写入套接字,将所有IAC字符加倍。
因此,使用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)