Python一步步telnetlib输出信息

Python step by step telnetlib output message

我正在使用 python.I 配置 ip 电话,可以通过 telnet 访问 ip 电话,但我想使用 python。

我的代码:

user = "admin"
password = "admin"
tn = telnetlib.Telnet("192.168.1.191",23)
output = tn.read_until("Login:")
tn.write("admin".encode("ascii")+ b"\r\n")
tn.read_until("Password:")
tn.write("admin".encode("ascii")+ b"\r\n")
tn.write("help".encode("ascii")+ b"\r\n")
output = tn.read_all()
tn.close()
print output

但这不起作用,因为它会导致无限循环而不打印任何输出。

提前感谢您的帮助。

根据 the documentation read_all blocks until the connection is closed. You'd probably be better off figuring out what the last line of output should look like (a prompt maybe?) and using read_until like you've been doing. You can also look into read_eager, read_very_eager or read_some.