python自动巡检H3C交换机

LczPtr 2020-04-10

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()

相关推荐