概述
腳本功能功能是全程模擬了人的手工巡檢,并收集數(shù)據(jù),處理后發(fā)送郵件。主要分為遠程到交換機,查看轉(zhuǎn)換機狀態(tài)如:show process cpu | in CPU utilization查看cpu占用率;將查詢后的結(jié)果記錄并保存下來,如結(jié)果;11%;讀取查詢的數(shù)據(jù)文件;將文件中的數(shù)據(jù)如11%提取部分需要計算出來;創(chuàng)建excel表格,將上一步獲取到的數(shù)據(jù)填寫到表格中;編寫固定格式的郵件內(nèi)容,并將保存好的excel表格附到郵件中發(fā)送;編寫crontab定時任務,實現(xiàn)全程自動化。
流程圖
'''
Author:myfu
Date:2019-08-26
E-mail:myfu@****.com
'''
importpexpect
importsys
importtime
importos
importxlwt
defcisco_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")
foriinrange(0,len(ip)):
child=pexpect.spawn('sshfumy@%s'%ip[i])
fout=open('/root/switch_check/%s.txt'%(name[i][:-1]),'wb+')
child.logfile=fout
ifip[i]=="10.3.240.21":
child.expect("fumy@10.3.240.21'spassword:")
else:
child.expect('password:')
child.sendline("你自己的密碼")
child.expect("%s"%name[i])
child.sendline("shenvpower")
child.expect("%s"%name[i])
child.sendline("showprocesscpu|inCPUutilization")
child.expect("%s"%name[i])
child.sendline("shversion|inuptime")
child.expect("%s"%name[i])
child.sendline("showprocessesmemory|inUsed")
child.expect("%s"%name[i])
child.sendline("shenvfan")
child.expect("%s"%name[i])
child.sendline("shlog|inConsolelogging")
child.expect("%s"%name[i])
child.sendline("exit")
time.sleep(3)
file_list=os.listdir("/root/switch_check")
withopen("/root/switch_check/%s"%file_list[0])asf1:
foriinf1:
if"POWER"ini:
power1=i[8:].strip()
if"CPUutilization"ini:
cpu1=i[72:].strip()
if"%suptimeis"%file_list[0][:-4]ini:
uptime1="".join(i[27:].strip().split(","))
if"ProcessorPoolTotal:"ini:
memory1="%.2f%%"%((int(i[38:50].strip())/int(i[21:33].strip()))*100)
if"FANis"ini:
fan1=i[6:].strip()
if"leveldebugging"ini:
log1=i[37:43].strip()
withopen("/root/switch_check/%s"%file_list[1])asf2:
foriinf2:
if"POWER"ini:
power2=i[8:].strip()
if"CPUutilization"ini:
cpu2=i[72:].strip()
if"%suptimeis"%file_list[1][:-4]ini:
uptime2="".join(i[27:].strip().split(","))
if"ProcessorPoolTotal:"ini:
memory2="%.2f%%"%((int(i[38:50].strip())/int(i[21:33].strip()))*100)
if"FANis"ini:
fan2=i[6:].strip()
if"leveldebugging"ini:
log2=i[37:43].strip()
withopen("/root/switch_check/%s"%file_list[2])asf3:
foriinf3:
if"POWER"ini:
power3=i[8:].strip()
if"CPUutilization"ini:
cpu3=i[72:].strip()
if"%suptimeis"%file_list[2][:-4]ini:
uptime3="".join(i[27:].strip().split(","))
if"ProcessorPoolTotal:"ini:
memory3="%.2f%%"%((int(i[38:50].strip())/int(i[21:33].strip()))*100)
if"FANis"ini:
fan3=i[6:].strip()
if"leveldebugging"ini:
log3=i[37:43].strip()
withopen("/root/switch_check/%s"%file_list[3])asf4:
foriinf4:
if"POWER"ini:
power4=i[24:].strip()
if"CPUutilization"ini:
cpu4=i[72:].strip()
if"%suptimeis"%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"FANis"ini:
fan4=i[6:].strip()
if"leveldebugging"ini:
log4=i[37:45].strip()
withopen("/root/switch_check/%s"%file_list[4])asf5:
foriinf5:
if"POWER"ini:
power5=i[24:].strip()
if"CPUutilization"ini:
cpu5=i[72:].strip()
if"%suptimeis"%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"FANis"ini:
fan5=i[6:].strip()
if"leveldebugging"ini:
log5=i[37:44].strip()
workbook=xlwt.Workbook(encoding='utf-8')
cisco=workbook.add_sheet('深圳網(wǎng)絡(luò)設(shè)備巡檢')
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=1
borders.right=1
borders.top=1
borders.bottom=1
style.borders=borders
al=xlwt.Alignment()
al.horz=0x02
al.vert=0x01
style.alignment=al
style1=xlwt.XFStyle()
borders=xlwt.Borders()
borders.left=1
borders.right=1
borders.top=1
borders.bottom=1
style1.borders=borders
al=xlwt.Alignment()
al.horz=0x02
al.vert=0x01
style1.alignment=al
style3=xlwt.XFStyle()
borders=xlwt.Borders()
borders.left=1
borders.right=1
borders.top=1
borders.bottom=1
style3.borders=borders
al=xlwt.Alignment()
al.horz=0x02
al.vert=0x01
style3.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*25
sec_col.width=120*25
thr_col.width=120*25
for_col.width=320*25
cisco.write(0,0,'設(shè)備名稱',style)
cisco.write(0,1,'管理地址',style)
cisco.write(0,2,'檢查項',style)
cisco.write(0,3,'檢查結(jié)果',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,'電源狀態(tài)',style1)
cisco.write(2,2,'cpu使用率',style1)
cisco.write(3,2,'運行時間',style1)
cisco.write(4,2,'內(nèi)存使用率',style1)
cisco.write(5,2,'風扇狀態(tài)',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,'電源狀態(tài)',style1)
cisco.write(8,2,'cpu使用率',style1)
cisco.write(9,2,'運行時間',style1)
cisco.write(10,2,'內(nèi)存使用率',style1)
cisco.write(11,2,'風扇狀態(tài)',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,'電源狀態(tài)',style1)
cisco.write(14,2,'cpu使用率',style1)
cisco.write(15,2,'運行時間',style1)
cisco.write(16,2,'內(nèi)存使用率',style1)
cisco.write(17,2,'風扇狀態(tài)',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,'電源狀態(tài)',style1)
cisco.write(20,2,'cpu使用率',style1)
cisco.write(21,2,'運行時間',style1)
cisco.write(22,2,'內(nèi)存使用率',style1)
cisco.write(23,2,'風扇狀態(tài)',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,'電源狀態(tài)',style1)
cisco.write(26,2,'cpu使用率',style1)
cisco.write(27,2,'運行時間',style1)
cisco.write(28,2,'內(nèi)存使用率',style1)
cisco.write(29,2,'風扇狀態(tài)',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-scheck_test你的郵箱/root/python/mail.txt?"%strf_time)
os.system("find/root/python/-typef-mtime+15-name'SZ_NDC_*'|xargs
m-f")
cisco_check()
mail.txt中的內(nèi)容即郵件內(nèi)容:
各位好:
巡檢結(jié)果詳情請看附件。
ps:本郵件由系統(tǒng)自動發(fā)送,如有任何問題請聯(lián)系***
BestWishes
郵件配置
/etc/mail.rc
setfrom=郵箱地址smtp=郵箱服務器smtp-auth-user=郵箱用戶名smtp-auth-password=密碼smtp-auth=login
定時任務
crontab-e
0017**5/usr/bin/python3/root/python/switch_check.py>/dev/null2>&1#每周五下午5點巡檢
最終效果
審核編輯 :李倩
-
自動化
+關(guān)注
關(guān)注
29文章
5578瀏覽量
79282 -
python
+關(guān)注
關(guān)注
56文章
4797瀏覽量
84690 -
腳本
+關(guān)注
關(guān)注
1文章
389瀏覽量
14866
原文標題:如何使用Python對交換機進行自動化巡檢?聰明的網(wǎng)工已經(jīng)收藏了!
文章出處:【微信號:網(wǎng)絡(luò)技術(shù)干貨圈,微信公眾號:網(wǎng)絡(luò)技術(shù)干貨圈】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論