【发布时间】:2013-07-20 20:41:08
【问题描述】:
我正在使用 Python telnetlib 从路由器获取“运行配置”输出。如何将“显示运行配置”输出存储在变量中。并打印变量。我的要求是每个输出都将显示在执行每一行代码时的控制台。是否有任何选项可以避免这些打印语句。
import telnetlib
#import getpass
ipaddr = "10.1.1.1"
passwd = "abcd123"
tn = telnetlib.Telnet(ipaddr)
if Password:
try:
print (tn.write (password + "\n"))
print(tn.read_until("Router>"))
print(tn.write("enable\n"))
print(tn.read_until("Password:"))
print(tn.write(passwd + "\n"))
print(tn.read_until("Router#"))
print(tn.write("show clock\n"))
print(tn.read_until("#"))
print(tn.write("show running-config\n"))
print(tn.write("\040\n"))
print(tn.write("\040\n"))
print(tn.write("\040\n"))
print(tn.read_until("#"))
print(tn.write("logout\n"))
print(tn.read_until(">"))
print tn.close
【问题讨论】:
-
您不一定需要打印每个操作;您可以将它们存储在变量、对象或字典之类的东西中,然后再执行打印操作。不过,从外观上看,它似乎非常具有事务性,因此可能无法变得更加优雅。如果你能提供一个输出样本,那是可能的。