python - 我可以清除 Python 2.7 上 Telnetlib 中的输出缓冲区吗

标签 python telnet telnetlib

我正在使用 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/

相关文章:

c - 超越 C telnet 服务器中的文本消息

ssh - Telnet 相对于 SSH 的优势?

python - 使用Python检查Telnet是否仍然连接

python - 如何使用Python和Selenium库与深度嵌套的 'input' html对象交互

python - 访问自定义 python 函数所在的文件夹

python - 从 MySQL 写入文件时出现内存问题

HTTP 保活超时

python - 如果字符串中有空格,则在 telnetlib write 命令中将字符串与文本组合不起作用

python - 在 Python 3.2 上清除 telnetlib 中的输出缓冲区

python - 模板文件更改时重新加载 Flask 应用程序