python 交换机巡检脚本_python交换机自动化巡检

此脚本实现通过SSH连接到多个Cisco网络设备进行状态检查,包括电源、CPU利用率、运行时间、内存使用率、风扇状态和日志条目。检查结果保存到Excel工作簿中,并通过电子邮件发送报告。此外,还删除15天以上的旧文件。
摘要由CSDN通过智能技术生成

'''Author:myfu

Date:2019-08-26

E-mail:myfu@****.com'''

importpexpectimportsysimporttimeimportosimportxlwtdefcisco_check():

ip=["10.3.240.14","10.3.240.15","10.3.240.17","10.3.240.20","10.3.240.21"]

name=["SZNS-KJY4F-ACS-05#","SZNS-KJY4F-ACS-06#","SZNS-KJY4F-ACS-08#","SZNS-KJY9F-ACS-11#","SZNS-KJY9F-ACS-12#"]

stat_time=time.time()

stru_time=time.localtime(stat_time)

strf_time=time.strftime("%Y-%m-%d")for i inrange(0,len(ip)):

child=pexpect.spawn('ssh fumy@%s'%ip[i])

fout=open('/root/switch_check/%s.txt'%(name[i][:-1]),'wb+')

child.logfile=foutif ip[i]=="10.3.240.21":

child.expect("fumy@10.3.240.21's password:")else:

child.expect('password:')

child.sendline("你自己的密码")

child.expect("%s"%name[i])

child.sendline("sh env power")

child.expect("%s"%name[i])

child.sendline("show process cpu | in CPU utilization")

child.expect("%s"%name[i])

child.sendline("sh version | in uptime")

child.expect("%s"%name[i])

child.sendline("show processes memory | in Used")

child.expect("%s"%name[i])

child.sendline("sh env fan")

child.expect("%s"%name[i])

child.sendline("sh log | in Console logging")

child.expect("%s"%name[i])

child.sendline("exit")

time.sleep(3)

file_list=os.listdir("/root/switch_check")

with open("/root/switch_check/%s"%file_list[0]) as f1:for i inf1:if "POWER" ini:

power1=i[8:].strip()if "CPU utilization" ini:

cpu1=i[72:].strip()if "%s uptime is"%file_list[0][:-4] ini:

uptime1="".join(i[27:].strip().split(","))if "Processor Pool Total:" ini:

memory1="%.2f%%"%((int(i[38:50].strip())/int(i[21:33].strip()))*100)if "FAN is" ini:

fan1=i[6:].strip()if "level debugging" ini:

log1=i[37:43].strip()

with open("/root/switch_check/%s"%file_list[1]) as f2:for i inf2:if "POWER" ini:

power2=i[8:].strip()if "CPU utilization" ini:

cpu2=i[72:].strip()if "%s uptime is"%file_list[1][:-4] ini:

uptime2="".join(i[27:].strip().split(","))if "Processor Pool Total:" ini:

memory2="%.2f%%"%((int(i[38:50].strip())/int(i[21:33].strip()))*100)if "FAN is" ini:

fan2=i[6:].strip()if "level debugging" ini:

log2=i[37:43].strip()

with open("/root/switch_check/%s"%file_list[2]) as f3:for i inf3:if "POWER" ini:

power3=i[8:].strip()if "CPU utilization" ini:

cpu3=i[72:].strip()if "%s uptime is"%file_list[2][:-4] ini:

uptime3="".join(i[27:].strip().split(","))if "Processor Pool Total:" ini:

memory3="%.2f%%"%((int(i[38:50].strip())/int(i[21:33].strip()))*100)if "FAN is" ini:

fan3=i[6:].strip()if "level debugging" ini:

log3=i[37:43].strip()

with open("/root/switch_check/%s"%file_list[3]) as f4:for i inf4:if "POWER" ini:

power4=i[24:].strip()if "CPU utilization" ini:

cpu4=i[72:].strip()if "%s uptime is"%file_list[3][:-4] ini:

uptime4="".join(i[27:].strip().split(","))if "Total:" ini:

memory4="%.2f%%"%((int(i[21:29].strip())/int(i[6:14].strip()))*100)if "FAN is" ini:

fan4=i[6:].strip()if "level debugging" ini:

log4=i[37:45].strip()

with open("/root/switch_check/%s"%file_list[4]) as f5:for i inf5:if "POWER" ini:

power5=i[24:].strip()if "CPU utilization" ini:

cpu5=i[72:].strip()if "%s uptime is"%file_list[4][:-4] ini:

uptime5="".join(i[27:].strip().split(","))if "Total:" ini:

memory5="%.2f%%"%((int(i[21:29].strip())/int(i[6:14].strip()))*100)if "FAN is" ini:

fan5=i[6:].strip()if "level debugging" ini:

log5=i[37:44].strip()

workbook= xlwt.Workbook(encoding = 'utf-8')

cisco= workbook.add_sheet('深圳网络设备巡检')

style=xlwt.XFStyle()

pattern=xlwt.Pattern()

pattern.pattern=xlwt.Pattern.SOLID_PATTERN

pattern.pattern_fore_colour= xlwt.Style.colour_map['red']

style.pattern=pattern

borders=xlwt.Borders()

borders.left= 1borders.right= 1borders.top= 1borders.bottom= 1style.borders=borders

al=xlwt.Alignment()

al.horz= 0x02al.vert= 0x01style.alignment=al

style1=xlwt.XFStyle()

borders=xlwt.Borders()

borders.left= 1borders.right= 1borders.top= 1borders.bottom= 1style1.borders=borders

al=xlwt.Alignment()

al.horz= 0x02al.vert= 0x01style1.alignment=al

style3=xlwt.XFStyle()

borders=xlwt.Borders()

borders.left= 1borders.right= 1borders.top= 1borders.bottom= 1style3.borders=borders

al=xlwt.Alignment()

al.horz= 0x02al.vert= 0x01style3.alignment=al

first_col=cisco.col(0)

sec_col=cisco.col(1)

thr_col=cisco.col(2)

for_col=cisco.col(3)

first_col.width=200*25sec_col.width=120*25thr_col.width=120*25for_col.width=320*25cisco.write(0, 0,'设备名称',style)

cisco.write(0,1, '管理地址',style)

cisco.write(0,2, '检查项',style)

cisco.write(0,3, '检查结果',style)

cisco.write_merge(1,6,0,0,'%s'%file_list[0][:-4],style3)

cisco.write_merge(1,6,1,1,'%s'%ip[0],style3)

cisco.write(1,2, '电源状态',style1)

cisco.write(2,2, 'cpu使用率',style1)

cisco.write(3,2, '运行时间',style1)

cisco.write(4,2, '内存使用率',style1)

cisco.write(5,2, '风扇状态',style1)

cisco.write(6,2, '日志条目',style1)

cisco.write(1,3, power1,style3)

cisco.write(2,3, cpu1,style3)

cisco.write(3,3, uptime1,style3)

cisco.write(4,3, memory1,style3)

cisco.write(5,3, fan1,style3)

cisco.write(6,3, log1,style3)

cisco.write_merge(7,12,0,0,'%s'%file_list[1][:-4],style3)

cisco.write_merge(7,12,1,1,'%s'%ip[1],style3)

cisco.write(7,2, '电源状态',style1)

cisco.write(8,2, 'cpu使用率',style1)

cisco.write(9,2, '运行时间',style1)

cisco.write(10,2, '内存使用率',style1)

cisco.write(11,2, '风扇状态',style1)

cisco.write(12,2, '日志条目',style1)

cisco.write(7,3, power2,style3)

cisco.write(8,3, cpu2,style3)

cisco.write(9,3, uptime2,style3)

cisco.write(10,3, memory2,style3)

cisco.write(11,3, fan2,style3)

cisco.write(12,3, log2,style3)

cisco.write_merge(13,18,0,0,'%s'%file_list[2][:-4],style3)

cisco.write_merge(13,18,1,1,'%s'%ip[2],style3)

cisco.write(13,2, '电源状态',style1)

cisco.write(14,2, 'cpu使用率',style1)

cisco.write(15,2, '运行时间',style1)

cisco.write(16,2, '内存使用率',style1)

cisco.write(17,2, '风扇状态',style1)

cisco.write(18,2, '日志条目',style1)

cisco.write(13,3, power3,style3)

cisco.write(14,3, cpu3,style3)

cisco.write(15,3, uptime3,style3)

cisco.write(16,3, memory3,style3)

cisco.write(17,3, fan3,style3)

cisco.write(18,3, log3,style3)

cisco.write_merge(19,24,0,0,'%s'%file_list[3][:-4],style3)

cisco.write_merge(19,24,1,1,'%s'%ip[3],style3)

cisco.write(19,2, '电源状态',style1)

cisco.write(20,2, 'cpu使用率',style1)

cisco.write(21,2, '运行时间',style1)

cisco.write(22,2, '内存使用率',style1)

cisco.write(23,2, '风扇状态',style1)

cisco.write(24,2, '日志条目',style1)

cisco.write(19,3, power4,style3)

cisco.write(20,3, cpu4,style3)

cisco.write(21,3, uptime4,style3)

cisco.write(22,3, memory4,style3)

cisco.write(23,3, fan4,style3)

cisco.write(24,3, log4,style3)

cisco.write_merge(25,30,0,0,'%s'%file_list[4][:-4],style3)

cisco.write_merge(25,30,1,1,'%s'%ip[4],style3)

cisco.write(25,2, '电源状态',style1)

cisco.write(26,2, 'cpu使用率',style1)

cisco.write(27,2, '运行时间',style1)

cisco.write(28,2, '内存使用率',style1)

cisco.write(29,2, '风扇状态',style1)

cisco.write(30,2, '日志条目',style1)

cisco.write(25,3, power5,style3)

cisco.write(26,3, cpu5,style3)

cisco.write(27,3, uptime5,style3)

cisco.write(28,3, memory5,style3)

cisco.write(29,3, fan5,style3)

cisco.write(30,3, log5,style3)

workbook.save('SZ_NDC_%s.xls'%strf_time)

os.system("mail -a /root/python/SZ_NDC_%s.xls -s check_test 你的邮箱 < /root/python/mail.txt"%strf_time)

os.system("find /root/python/ -type f -mtime +15 -name 'SZ_NDC_*'|xargs \rm -f")

cisco_check()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值