我正在使用 telnetlib 打印我写入服务器的最后一条命令后的输出
tn.write(cmd_login)
tn.write(cmd...)
tn.write(cmd_last)
print tn.expect([word],timeout)[-1]
然而,当我打印期望的返回时,它也显示了我之前写到服务器的结果(例如:cmd_login cmd ...) 无论如何只在 tn.write(cmd_last) 之后打印结果?
最佳答案
我建议在每次 Telnet.write 之后使用 Telnet.read_until。对于 read_until 的预期参数,可以使用 telnet 提示符。最后,应该从输出中减去命令。我不知道更好的方法。它可能看起来像这样:
tn.write(cmd_login)
tn.read_until(prompt)
tn.write(cmd...)
tn.read_until(prompt)
tn.write(cmd_last)
output = tn.read_until(prompt)
cmdIndex = output.find(cmd_last)
output = output[cmdIndex + len(cmd_last):]
print output
关于python - 我可以清除 Python 2.7 上 Telnetlib 中的输出缓冲区吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16312960/