python telnetlib清除缓冲区

问题描述 投票:2回答:1

我有这个telnet到我的路由器的脚本。我想用这个脚本保存各种show命令输出。然而,似乎一旦我将第一个命令写入缓冲区,我就无法清除它。例如,在脚本中它将命令“show isis adj”写入缓冲区,然后在脚本中我想运行另一个show命令并保存该输出。这里的问题似乎是缓冲区只是两次保存“show isis adj”的第一个命令?如何清除此缓冲区以便运行多个show命令?

此外,我确实阅读了其他一些堆栈溢出帖子,他们使用“Telnet.read_until(期望[,超时])引用”但是我尝试了一些,但我仍然无法让它工作。

import telnetlib
import datetime


now = datetime.datetime.now()

host = "x.x.x.x" # your router ip
username = "root" # the username
password = "root"


print("""#######################
  CONNECTING TO DEVICE""")

tn = telnetlib.Telnet(host)
tn.read_until("Username:")
tn.write(username+"\n")
tn.read_until("Password:")
tn.write(password+"\n")
tn.write("terminal length 0"+"\n")


print("""#######################
  SHOWING ISIS ADJ""")

tn.write("show isis adjacency"+"\n")
tn.write("exit"+"\n")
output1 = tn.read_until("cmd")
print(output1)


print("""#######################
  SHOWING BGP SESSIONS""")

tn.write("show bgp sessions"+"\n")
tn.write("exit"+"\n")
output2 = tn.read_until("cmd")
print(output1)
python python-2.7
1个回答
0
投票

你打印两次output1!修复它并使用output2

....
print("""#######################
  SHOWING BGP SESSIONS""")

tn.write("show bgp sessions"+"\n")
tn.write("exit"+"\n")
output2 = tn.read_until("cmd")
print(output2)
© www.soinside.com 2019 - 2024. All rights reserved.