1.需求分析
1)手动巡检还是美滋滋,不需要动脑
2)因为之前看到文章有大佬用pexpect.spawn()来连接网络设备,然后试了一下,发现要linux系统才行,然后就研究出了这个。
2.telnet交换机(cisco)输入命令并导出:源码实现
def telnetip(ip):
# 连接Telnet服务器
tn = telnetlib.Telnet(ip, port=23, timeout=50)
# 输入登录用户名
fout=open('text1111.txt','w')
tn.logfile=fout
tn.read_until(b'Username:')
tn.write(username + b'\n')
# 输入登录密码
# tn.read_until('Password:')
tn.write(password + b'\n')
time.sleep(1)
tn.write(b'show memory' + b'\n') # 查看内存
tn.write(b' ' + b'\n')#敲空格,不解释
tn.write(b' ' + b'\n')
tn.write(b'show processes cpu sorted' + b'\n') #查看CPU
tn.write(b' ' + b'\n')
tn.write(b' ' + b'\n')
tn.write(b'show environment' + b'\n') #查看温度
tn.write(b' ' + b'\n')
tn.write(b' ' + b'\n')
tn.write(b' ' + b'\n')
tn.write(b'show ip routing' + b'\n') #查看路由表
tn.write(b' ' + b'\n')
tn.write(b' ' + b'\n')
tn.write(b&