Python自动巡检H3C交换机实现过程解析
1.通过netmiko模块登录交换机,协议ssh,执行收集信息命令,保存至txt文件
2.过滤txt文件中的内容,保存到excel,使用xlwt模块实现。
3.sendmai发送excel邮件。或者发送给钉钉机器人也可以
4.使用crond定时发送巡检报表。
代码如下
#!/usr/bin/python3 #H3c交换机 import time from netmiko import ConnectHandler now = time.strftime("%Y%m%d",time.localtime(time.time())) log_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) ip_list = [ ['sw-001','192.168.1.1'], ['sw-002','192.168.1.2'], ] SW = { 'device_type':'hp_comware', 'username':'admin', 'ip':'', 'password':"$password" } for ip_item in ip_list: SW['ip'] = ip_item[1] connect = ConnectHandler(**SW) print(log_time + 'Successfully connected to ' + ip_item[0]) output = connect.send_command('system view') iproute = connect.send_command("display ip routing-table") version = connect.send_command('dis version') fan = connect.send_command('display fan') cpu = connect.send_command('display cpu-usage') mem = connect.send_command('display memory') env = connect.send_command('display environment') fo = open('xusj','w') fo.write(iproute) fo.write(fan) fo.write(cpu) fo.write(mem) fo.write(env) fo.close()
相关推荐
shyoldboy 2020-09-27
thlm0 2020-08-15
Echodat 2020-08-05
BingGoGo技术 2020-07-25
BraveWangDev 2020-07-19
woaishanguosha 2020-07-18
OnMyHeart 2020-06-16
divingduck 2020-06-14
TinyDolphin 2020-06-13
xiaogoua 2020-06-12
STchaoL 2020-06-11
cj0 2020-06-07
waitzkj 2020-06-06
xiaoyezihanghui 2020-05-15
架构技术交流 2020-05-08
shenzhenzsw 2020-05-01
Terminator0 2020-04-29