我写了一个类似的代码并得到了类似的错误。然后我让代码发声以知道我在哪里犯了错误。我得出的结论是:“一直使用 read_all() 函数并不是一个好主意。它会无限读取并带来类似挂起模式的印象。尝试在读取过程中将其替换为设备提示,然后是计时器。并尝试打印它以查看是否代码捕获了所需的输出”
import telnetlib
import os
import sys
host = raw_input("Enter the VG IP : ")
user = "cisco"
password = "cisco"
#cmd = raw_input("Enter the command you want to feed : ")
cmd1 = "term len 0"
cmd = "show clock"
pingable = False
response = os.system("ping -c 2 " + host)
if response == 0:
pingable = True
print(host, "is Pingable", pingable)
else:
print(host, "is un-Pingable", pingable)
if(pingable):
tn = telnetlib.Telnet(host)
banner = tn.read_until("Username:", 5)
tn.write(user + "\n")
print(banner)
tn.read_until("Password:", 5)
tn.write(password1 + "\n")
prompt = tn.read_until("#")
print("I am logged in\n\n")
print(prompt)
tn.write(cmd1 + b"\n")
output1 = tn.read_until("#",5)
print("my first cmd output is :", output1, "\n")
tn.write(cmd + "\n")
output1 = tn.read_until("#",5)
print("My 2nd cmd is feeded here", output1)
tn.write("show version\n")
output1 = tn.read_until("more-- ",5)
print("version info : ", output1)
tn.write("exit\n")
else:
print(host, "is unpingable")