Python3网络设备巡检(交换机篇)

您所在的位置:网站首页 网络设备巡检的主要内容 Python3网络设备巡检(交换机篇)

Python3网络设备巡检(交换机篇)

#Python3网络设备巡检(交换机篇)| 来源: 网络整理| 查看: 265

介绍

只写了交换机的,路由器、防火墙、负载等其它设备以后,有需求再写,下面以一组交换机为例。

思路

1、收取交换机的信息 需要哪些信息,哪些命令,不同品牌交换机命令不同(本篇以H3C为例),需要提前规划好。交换机的IP、名字、密码等信息,写入数据库比较方便。 2、把需要的信息填入表格 根据关键字提取交换机中的状态信息,并一次填入设计好的表格中,难点是字符串的截取和excel表格的设计。同款交换机相同的命令,可能用同一个关键字,抓取的信息也不一样,这个要多试几次。 3、编写邮件,发送邮件。 4、linux编写任务计划,crontab定期执行python代码,定期发送邮件。

代码

抓取交换机需要的配置信息

import pexpect import sys import datetime import pymssql import os today=datetime.date.today().strftime('%Y%m%d') path = "/root/xunjian/"+today os.mkdir(path,777)#创建目录 def Switch(name,ip,passwd): try:#try except 防止有一个命令错误,导致程序不能进行,其实不加也可以,如果有命令没输出,下一个代码也会报错。 name1="---- More ----"#模拟交换机出现的翻页提示 child=pexpect.spawn('telnet %s'%ip) fout=open('/root/xunjian/'+today+'/'+'%s-%s.txt'%(name,ip),'wb+') child.logfile = fout child.expect('login:')#提示用户登录,输入帐号,交换机不同,有所不同。 child.sendline("admin") child.expect('(?i)ssword:')#提示输入密码 child.sendline("%s"%passwd) child.expect(''%name) child.sendline("display cpu-usage")#查看cpu状态 child.expect(''%name) child.sendline("display memory")#查看内存状态 child.expect(''%name) child.sendline("display environment")#运行温度 child.expect(''%name) child.sendline("display fan")#风扇状态,一般输出都有2个 child.expect(''%name) child.sendline("display power")#电源状态 child.expect(''%name) child.sendline("display ip routing-table")#路由表 for i in range(10): index = child.expect([name1,''%name]) if ( index == 0 ): child.send(" ") else: child.sendline("display interface brief")#端口状态 break for i in range(10): index = child.expect([name1,''%name]) if ( index == 0 ): child.send(" ") else: child.sendline("dis version")#版本,为了看运行时间 break for i in range(10): index = child.expect([name1,''%name]) if ( index == 0 ): child.send(" ") else: child.sendline("display log")#日志,日志较多,循环100个空格,怕输出不全。 break for i in range(100): index = child.expect([name1,''%name]) if ( index == 0 ): child.send(" ") else: child.sendline("quit") break except: pass host = 'x.x.x.x'#连接数据库,抓取数据库内的信息,交换机的名字、ip、密码 user = 'sa' pwd = 'xxxx' db = 'MAC' conn = pymssql.connect(host=host,user=user,password=pwd,database=db,timeout=1,login_timeout=1,charset="utf8") cur = conn.cursor() sqls ="select * from [dbo].[F5HJSwitch]" cur.execute(sqls) listall = cur.fetchall()#SQl输出内容导成列表 for line in listall: Switch(line[1],line[2],line[3]) conn.commit() conn.close() 代码

提取需要的信息,并塞入表格,因为设备太多,我只展示一个设备的代码。

import pymssql import xlwt import datetime from xlwt import * today=datetime.date.today().strftime('%Y%m%d') txt='F51FA-HJ-S5560X-x.x.x.x.txt'#打开设备的配置信息,不建议用循环打开所有的,因为有些设备的输出会多空格等。 file = open('/root/xunjian/'+today+'/'+txt,'r+') listlist=file.readlines()#读取文档的每一行,至列表,如果不是双电源的问题,直接 for line in file.readlines(): i=1 for line in listlist:#读取列表每一行,因为两个电源的所有输出信息都一样,只能匹配关键字后输出下一行字符串,实属无奈。 if '1 Normal' in line: power11=line[8:15].rstrip()#设备状态,都是匹配关键字,然后截取本来的字符串输出,这个要多试几次。 print(power11)#确认输出是自己想要的字符串。 if 'Uptime is' in line:#运行时间 time11=line[-33:].rstrip() print(time11) if 'hotspot' in line: environment11=line[17:21].rstrip()#运行温度 print(environment11) if 'Fan 1:' in line: fana11=listlist[i+1][-8:].rstrip()#电源状态,匹配关键字,截取下一行的字符串 print(fana11) if 'Fan 2:' in line: fanb11=listlist[i+1][-8:].rstrip()#电源状态,匹配关键字,截取下一行的字符串 print(fanb11) if 'in last 5 minutes' in line: cpu11=line[6:10].rstrip()#cpu使用率 print(cpu11) if 'Mem:' in line: memory11=line[-7:].rstrip()#内存 print(memory11) if 'To_F5-Core-S12508_Ten-G1' in line:#端口 briefa11=line[20:30].rstrip() print(briefa11) if 'To_F5-Core-S12508_Ten-G2' in line: briefb11=line[20:30].rstrip() print(briefb11) if 'Current messages:' in line:#日志条目 log11=line[-5:].rstrip() print(log11) if 'Routes' in line: routingtable11=line[-5:].rstrip()#路由条目 print(routingtable11) i += 1 workbook = xlwt.Workbook()#创建表格 style = XFStyle()#初始化样式,此样式包含了单元格背景颜色和单元格边框两个属性。 pattern = Pattern() pattern.pattern = Pattern.SOLID_PATTERN pattern.pattern_fore_colour = Style.colour_map['blue'] #设置单元格背景色为蓝色 style.pattern = pattern borders = xlwt.Borders()#设置表格的边框,1是默认实线黑色。 borders.left = 1 borders.right = 1 borders.top = 1 borders.bottom = 1 style.borders = borders style1 = XFStyle()#只有边框 borders = xlwt.Borders() borders.left = 1 #borders.left = xlwt.Borders.THIN borders.right = 1 borders.top = 1 borders.bottom = 1 style1.borders = borders style3 = XFStyle()#初始化样式,带边框和表格内容居中。 borders = xlwt.Borders() borders.left = 1 #borders.left = xlwt.Borders.THIN borders.right = 1 borders.top = 1 borders.bottom = 1 style3.borders = borders al = xlwt.Alignment() al.horz = 0x02 # 设置水平居中 al.vert = 0x01 # 设置垂直居中 style3.alignment = al F51FSwitch = workbook.add_sheet('F51FSwitch',cell_overwrite_ok=True)#创建表格的某一分页 first_col=F51FSwitch.col(0)#设置0、1、2、3列的列宽 sec_col=F51FSwitch.col(1) thr_col=F51FSwitch.col(2) for_col=F51FSwitch.col(3) first_col.width=150*25 sec_col.width=100*25 thr_col.width=120*25 for_col.width=320*25 F51FSwitch.write_merge(1,11,0,0,'QCMC-F5-1FA',style3)#合并单元格(1,11为行1到11行 0,0为列0到0),填入内容 #F51FSwitch.write_merge(1,10,0,1,'QCMC-F3-1FA')#合并0到1列,1到10行 F51FSwitch.write_merge(1,11,1,1,'10.20.5.1',style3)#添加style3的样式,只能填写一个,所以初始化样式的时候根据需求添加多个属性。 F51FSwitch.write(0,0,'设备名称',style) F51FSwitch.write(0,1,'管理地址',style) F51FSwitch.write(0,2,'检查项',style) F51FSwitch.write(0,3,'检查结果',style) F51FSwitch.write(1,2,'设备状态',style1) F51FSwitch.write(2,2,'运行时间',style1) F51FSwitch.write(3,2,'运行温度',style1) F51FSwitch.write(4,2,'风扇A状态',style1) F51FSwitch.write(5,2,'风扇B状态',style1) F51FSwitch.write(6,2,'CPU使用率',style1) F51FSwitch.write(7,2,'内存使用率',style1) F51FSwitch.write(8,2,'聚合口A',style1) F51FSwitch.write(9,2,'聚合口B',style1) F51FSwitch.write(10,2,'日志条目',style1) F51FSwitch.write(11,2,'路由条目',style1) F51FSwitch.write(1,3,power11,style1)#添加抓取的字符串到相应的表格 F51FSwitch.write(2,3,time11,style1) F51FSwitch.write(3,3,environment11,style1) F51FSwitch.write(4,3,fana11,style1) F51FSwitch.write(5,3,fanb11,style1) F51FSwitch.write(6,3,cpu11,style1) F51FSwitch.write(7,3,memory11,style1) F51FSwitch.write(8,3,briefa11,style1) F51FSwitch.write(9,3,briefb11,style1) F51FSwitch.write(10,3,log11,style1) F51FSwitch.write(11,3,routingtable11,style1) print ('创建excel文件完成!') workbook.save('/root/xunjian/%sF5Switchxunjian.xls'%today) 发送邮件代码

前两个文章有介绍,不写了。

任务计划 linux crontab(不会的请百度)

crontab -l 查看任务计划 这里写图片描述 crontab -e编写任务计划 这里写图片描述

结果

这里写图片描述

吐槽

代码一直执行没问题,突然前两天突然不发邮件了。 发现没生成excel文档 但是交换机的txt文档还在 手动执行生成excel文档的代码,报错。说utf-8格式问题 在windows python下面执行是OK的 后来发现有一个txt的文档是ANSI格式,其它的txt都是UEF-8格式 实在不明白为啥会有个txt是ANSI格式的,然后一顿改代码,在linux下面就是不行。 后来无意中发现,交换机的运行时间怎么是三天???和代码出问题很相符啊!! 然后然后,重启了这台交换机,居然好了,正常了。 H3C交换机真坑??莫非是上次交换机冷重启的原因?现在正常重启后,正常了??吐血的一下午。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3