用python编写自动读取华为2501配置文件的脚本(单步可通过,直接编译就不通过)
import telnetlibimport sys
HOST5 = "11.156.23.32"
password1 = "passw1"
spacetime = 1
tn = telnetlib.Telnet(HOST5)
tn.read_until("Quidway>")
tn.write("en\n")
tn.read_until("Password:")
tn.write(password1+"\n")
tn.read_until("Password:")
tn.write(password1+"\n")
tn.read_until("Quidway#")
spacetime=1
tn.write("show run\n")#这一步就不能运行了,非常奇怪。
while spacetime <= 6:
tn.write(" ")
spacetime = spacetime + 1
msg_run=tn.read_until("Quidway#")
spacetime=1
tn.write("show int\n")
while spacetime <= 6:
tn.write(" ")
spacetime = spacetime + 1
#msg_int = tn.read_until("Quidway#")
msg_int=tn.read_until("Quidway#")
tn.write("exit\n")
path = r"E:\backup\BOSS_2501_PORT.TXT"
f = file(path,"wb")
f.write(msg_int)
path = r"E:\backup\BOSS_2501_CONFIG.TXT"
f = file(path,"wb")
f.write(msg_run)
f.close()
tn.close