chinese直男口爆体育生外卖, 99久久er热在这里只有精品99, 又色又爽又黄18禁美女裸身无遮挡, gogogo高清免费观看日本电视,私密按摩师高清版在线,人妻视频毛茸茸,91论坛 兴趣闲谈,欧美 亚洲 精品 8区,国产精品久久久久精品免费

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

Netmiko+excle定時(shí)檢測(cè)接口狀態(tài)

jf_yLA7iRus ? 來(lái)源:知乎楓嵐 ? 作者:知乎楓嵐 ? 2022-10-19 09:15 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

一、前言:在本人看著群里的大佬紛紛分享自己的文章和一些運(yùn)維思路,深知只有用出來(lái),寫出來(lái),分享出來(lái),跟大家一起交流,這個(gè)程序和學(xué)習(xí)到的新知識(shí)才算真正掌握了。在這里非常感謝王印王老師@弈心、朱嘉盛老師@朱嘉盛以及群里的各位大佬不遺余力地分享著自己的文章。針對(duì)python的學(xué)習(xí)主要源于他們的專欄。

二、實(shí)驗(yàn)背景:

前一段時(shí)間,機(jī)房剛剛建完,但是弱電做的網(wǎng)線不太行,出現(xiàn)兩次網(wǎng)線問(wèn)題,這不是要了命了么。領(lǐng)導(dǎo)說(shuō),想辦法如果交換機(jī)網(wǎng)線出問(wèn)題了你得能知道。我想說(shuō),咱們用個(gè)運(yùn)維平臺(tái)不行嗎,我又想我們今年手里的服務(wù)器資源只剩下4核,6G了。
行,那就寫個(gè)腳本吧。本次實(shí)驗(yàn)以H3C設(shè)備為基礎(chǔ)。

就是說(shuō)寫一個(gè)只要接口狀態(tài)發(fā)生變化就能有郵件通知你,而且還有excle作為佐證。

我認(rèn)為本次實(shí)驗(yàn)僅僅適用于十幾二十臺(tái)小環(huán)境的網(wǎng)絡(luò)。(而且有點(diǎn)錢上個(gè)平臺(tái)不比嘛強(qiáng))

三、需求分析

首先首次使用腳本之后,excle里工作表收集的都是當(dāng)前所有設(shè)備的UP的接口。

81f94692-4f45-11ed-a3b6-dac502259ad0.jpg

工作簿底下的工作表的是所有設(shè)備,以命名+IP的形式存在。

822ed49c-4f45-11ed-a3b6-dac502259ad0.jpg

工作簿底下是所有的設(shè)備

然后如果有一個(gè)接口的狀態(tài)出現(xiàn)了變化,只要有變化,Link這一列對(duì)應(yīng)的狀態(tài)就會(huì)變色,還會(huì)發(fā)郵件以工作簿為附件。

824087fa-4f45-11ed-a3b6-dac502259ad0.jpg

82770eec-4f45-11ed-a3b6-dac502259ad0.jpg

如果你是就想人為規(guī)劃斷開(kāi)這個(gè)接口,不是鏈路的損壞,那么在下次執(zhí)行這個(gè)腳本的時(shí)候,不是UP接口就不存在了。

82a1a1e8-4f45-11ed-a3b6-dac502259ad0.jpg

如果這個(gè)接口從ADM狀態(tài)又回復(fù)為UP,那么也會(huì)發(fā)郵件,并且攜帶工作簿為附件。

82affcb6-4f45-11ed-a3b6-dac502259ad0.jpg

81f94692-4f45-11ed-a3b6-dac502259ad0.jpg

最后設(shè)置成1分鐘執(zhí)行一次,放在服務(wù)器里。

每次執(zhí)行程序只會(huì)維護(hù)第一次執(zhí)行程序輸出的這一張表,所以不用擔(dān)心文件太多。

三、代碼分析

3.1完整代碼

先上完整代碼,然后再進(jìn)行分析。

#coding=gbk
importre
importsmtplib
importthreading
importtime
fromemail.mime.applicationimportMIMEApplication
fromemail.mime.multipartimportMIMEMultipart
fromemail.mime.textimportMIMEText
frompprintimportpprint
fromqueueimportQueue

fromnetmikoimportConnectHandler
fromopenpyxl.reader.excelimportload_workbook
fromopenpyxl.workbookimportWorkbook
fromopenpyxl.stylesimportPatternFill,Border,Side,Font

threads=[]#用于多線程
ip_list=open('ip_file.txt')#IP地址先行放入文件中
dims={}#用于對(duì)工作表中自動(dòng)設(shè)置最大行寬
content=""#用于輸出在郵箱中的內(nèi)容
defssh_seesion(ip,ouput,):
globalcontent,sheet
#這幾個(gè)列表是寫入工作表的先行條件,也就是工作表的每一列,先把想寫入工作表的每一列的內(nèi)容寫入列表,然后再遍歷列表把內(nèi)容寫入工作表
interface_list=[]
link_list=[]
speed_list=[]
description_list=[]

connection_info={'device_type':'hp_comware',
'ip':ip,
'username':'xxxxx',
'password':'xxxxxx'}

withConnectHandler(**connection_info)asconn:
output=conn.send_command("displayinterfacebrief",use_textfsm=True)
sysname=conn.send_command('displaycurrent-configuration|includesysname')
name=re.search(r's+S+s+(S+)',sysname).groups()[0]
#pprint(output)

try:
sheet=wb[name+'_'+ip]#調(diào)用自己的那一張表
#先給逼刪了
column_B=sheet['B']
foriincolumn_B:
#print(i.value)
ifi.valuenotin('Link','UP'):
num=re.search('d+',i.coordinate).group()#找到需要?jiǎng)h的那一行
print(num)
sheet.delete_rows(int(num))
except:
pass

#取出接口up的
foriinoutput:
ifi.get('link')=='UP':
interface_list.append(i.get('interface'))
link_list.append(i.get('link'))
speed_list.append(i.get('speed'))
description_list.append(i.get('description'))
#判斷這次interface跟上次也就是表格里的有沒(méi)有區(qū)別以是否是UP的為前提不是UP的或者多了UP的或者少了UP的只要變化就要被記錄


ifstr(name+'_'+ip)inwb.sheetnames:#如果這個(gè)表存在就讓里原本有的接口進(jìn)入列表
sheet_pre=wb[name+'_'+ip]
column_A=sheet_pre['A']#取出以前的表的第一列
sheet_pre_A1=[i.valueforiincolumn_A]
sheet_pre_A1.remove('Interfaces')#遍歷第一列的時(shí)候會(huì)有抬頭也就是Interfaces'需要把這個(gè)去掉
#print(sheet_pre_A1)#sheet_pre_A1里是上一次表格里有的接口列表
#print(interface_list)#interface_list里是這一次想放入表格里的UP的接口的列表
#取出兩個(gè)列表的差集,這個(gè)差集是現(xiàn)在UP的和表里的差集
sheet_dif=list(set(sheet_pre_A1)^(set(interface_list)))#把兩個(gè)表中變化的接口放入sheet_dif這個(gè)列表里
print(sheet_dif)#至此有變化的且不是UP的接口就進(jìn)入列表了
iflen(sheet_dif)!=0:#如果這個(gè)列表里有數(shù)據(jù)就發(fā)郵箱
content=content+f"{name}{str(sheet_dif)}接口發(fā)生了變化
'"#配合發(fā)郵件的

foriinoutput:#為了把差集的接口情況寫入列表
forpinsheet_dif:
ifp==i.get('interface')andi.get('link')!='UP':#找到這個(gè)不是UP的接口各種情況還寫進(jìn)去
interface_list.append(i.get('interface'))
link_list.append(i.get('link'))
speed_list.append(i.get('speed'))
description_list.append(i.get('description'))
content=content+i.get('interface')+'接口由UP變成了'+i.get('link')+'
'
elifp==i.get('interface')andi.get('link')=='UP':
content=content+i.get('interface')+'接口UP了'+'
'


font=Font(name="微軟雅黑",bold=True)#字體加粗
yellowFill=PatternFill(start_color='FFFF00',end_color='FFFF00',fill_type='solid')#黃色
thin_border=Border(left=Side(style='thin'),right=Side(style='thin'),top=Side(style='thin'),
bottom=Side(style='thin'))#有邊框
SpringGreen=PatternFill(start_color='3CB371',end_color='3CB371',fill_type='solid')#黃色

ifstr(name+'_'+ip)inwb.sheetnames:#如果表格存在直接往里寫
row_numbers=list(range(2,len(output)+2))#只能從第二行開(kāi)始
forinterface,rowinzip(interface_list,row_numbers):
sheet.cell(row=row,column=1,value=interface)
forlink,rowinzip(link_list,row_numbers):
sheet.cell(row=row,column=2,value=link)
forspeed,rowinzip(speed_list,row_numbers):
sheet.cell(row=row,column=3,value=speed)
fordescription,rowinzip(description_list,row_numbers):
sheet.cell(row=row,column=4,value=description)

#往里寫完之后查看B1這列然后找到不是UP的給賦值綠色
column_B=sheet['B']
foriincolumn_B:
#print(i.value)
ifi.valuenotin('Link','UP'):
print(i.coordinate)#查找到接口有問(wèn)題的坐標(biāo)
sheet[i.coordinate].fill=SpringGreen


else:#如果表格不存在則創(chuàng)建表格
sheet=wb.create_sheet(name+'_'+ip)#這里的sheet相當(dāng)于JT-6-1F-DAS-1這個(gè)表格
columns=['A1','B1','C1','D1']
cells=['Interfaces','Link','Speed','Description']
fori,pinzip(columns,cells):
#放入表格中
sheet[i]=p
sheet[i].fill=yellowFill
sheet[i].font=font

row_numbers=list(range(2,len(output)+2))#只能從第二行開(kāi)始
forinterface,rowinzip(interface_list,row_numbers):
sheet.cell(row=row,column=1,value=interface)
forlink,rowinzip(link_list,row_numbers):
sheet.cell(row=row,column=2,value=link)
forspeed,rowinzip(speed_list,row_numbers):
sheet.cell(row=row,column=3,value=speed)
fordescription,rowinzip(description_list,row_numbers):
sheet.cell(row=row,column=4,value=description)


forrowinsheet.rows:
#print(row)
forcellinrow:
#print(cell.value)
cell.border=thin_border
ifcell.value:
dims[cell.column_letter]=max((dims.get(cell.column_letter,0),len(str(cell.value))))

forcol,valueindims.items():
sheet.column_dimensions[col].width=value+3


defsend_email(sender,receicer,password,content):
#這份代碼比較標(biāo)準(zhǔn)了,可以直接用了

#發(fā)件人郵箱
sender=sender
#收件人郵箱
receiver=receicer
#抄送人郵箱
#acc='xxxxxxxx@qq.com'
#郵件主題
subject='服務(wù)器運(yùn)行情況'

#郵箱密碼(授權(quán)碼)
password=password

#郵件設(shè)置
msg=MIMEMultipart()
msg['Subject']=subject#主題
msg['to']=receiver#接收者
#msg['acc']=acc#抄送者
msg['from']="信息化員工"#發(fā)件人

#郵件正文
content=content

#添加郵件正文:
msg.attach(MIMEText(content,'plain','utf-8'))#content是正文內(nèi)容,plain即格式為正文,utf-8是編碼格式

#添加附件
#注意這里的文件路徑是斜杠
file_name=r'E:python	est	est功能腳本接口up_down	est_openpyxl.xlsx'
file_name_list=file_name.split('\')[-1]#獲得文件的名字
xlsxpart=MIMEApplication(open(file_name,'rb').read())
xlsxpart.add_header('Content-Disposition','attachment',filename=file_name_list)
#服務(wù)端向客戶端游覽器發(fā)送文件時(shí),如果是瀏覽器支持的文件類型,一般會(huì)默認(rèn)使用瀏覽器打開(kāi),比如txt、jpg等,會(huì)直接在瀏覽器中顯示,如果需要提示用戶保存,就要利用Content-Disposition進(jìn)行一下處理,關(guān)鍵在于一定要加上attachment
msg.attach(xlsxpart)

#設(shè)置郵箱服務(wù)器地址以及端口
smtp_server="smtp.qq.com"
smtp=smtplib.SMTP(smtp_server,25)#'smtp.qq.com'是QQ郵箱發(fā)郵件的服務(wù)器,用新浪郵箱就是'smtp.sina.com',就是smtp加上你們郵箱賬號(hào)@符號(hào)后面的內(nèi)容。端口默認(rèn)是25。
#smtp.set_debuglevel(1)#顯示出交互信息

#登陸郵箱
smtp.login(sender,password)

#發(fā)送郵件
smtp.sendmail(sender,receiver.split(','),msg.as_string())
#receiver.split(',')+acc.split(',')是['xxxxxxxx@qq.com','xxxxxxxx@qq.com']

#斷開(kāi)服務(wù)器鏈接
smtp.quit()

print(f"程序于{time.strftime('%X')}執(zhí)行開(kāi)始
")
#記錄開(kāi)始時(shí)間
start_time=time.time()

#注意邏輯關(guān)系先創(chuàng)建工作簿再進(jìn)入多線程最后保存工作簿
try:#如果存在這個(gè)表格就直接打開(kāi),如果部存在就創(chuàng)建
wb=load_workbook('test_openpyxl.xlsx')
ws=wb.active
except:#創(chuàng)建表格如果存在就不創(chuàng)建
wb=Workbook()
wb.remove(wb['Sheet'])
ws=wb.active

foripsinip_list.readlines():
t=threading.Thread(target=ssh_seesion,args=(ips.strip(),Queue()))
t.start()
threads.append(t)


foriinthreads:
i.join()
#加入檢查功能


iflen(content)!=0:
print(content)
send_email("xxxx@qq.com","xxxx@qq.com","jveyorpbogllijhj",content)


end_time=time.time()-start_time

wb.save('test_openpyxl.xlsx')#保存工作表
print(f'總共耗時(shí){round(end_time,2)}秒')
print(f"程序于{time.strftime('%X')}執(zhí)行結(jié)束
")
TestFSM模板
ValueInterface(S+)
ValueLink(UP|DOWN|ADM|Stby)
ValueSpeed(.*G|auto)
ValueDescription(S+|s+)

Start
^s*${Interface}s+${Link}s+${Speed}((a)|s*)+s+S+s+S+s+S+s+${Description}->Record

3.2分析

特別詳細(xì)的分析寫在了代碼的注釋中。這里只是對(duì)思路的分析。

首先就是登錄設(shè)備,然后調(diào)用TestFSM模板做解析,再取出設(shè)備的名字。use_textfsm=True的用法參照朱嘉盛:《網(wǎng)絡(luò)工程師的Python之路》(nornir實(shí)驗(yàn)10,聯(lián)動(dòng)Textfsm,ntc-template,華為)

defssh_seesion(ip,ouput,):
globalcontent,sheet
#這幾個(gè)列表是寫入工作表的先行條件,也就是工作表的每一列,先把想寫入工作表的每一列的內(nèi)容寫入列表,然后再遍歷列表把內(nèi)容寫入工作表
interface_list=[]
link_list=[]
speed_list=[]
description_list=[]

connection_info={'device_type':'hp_comware',
'ip':ip,
'username':'xxxxxx',
'password':'123'}

withConnectHandler(**connection_info)asconn:
output=conn.send_command("displayinterfacebrief",use_textfsm=True)
sysname=conn.send_command('displaycurrent-configuration|includesysname')
name=re.search(r's+S+s+(S+)',sysname).groups()[0]
#pprint(output)

首先明確這個(gè)腳本是一分鐘執(zhí)行一次,然后是在工作簿中找到此次登錄的設(shè)備的工作表,然后對(duì)其進(jìn)行刪除操作,刪除工作表中存在的不是UP的接口的那一行,因?yàn)檫@個(gè)工作簿的目的是存接口為UP的接口的信息,那么之前存在不是UP的接口的內(nèi)個(gè)工作簿呢?通過(guò)郵箱發(fā)出來(lái)了。因?yàn)槿绻谝淮螆?zhí)行這個(gè)程序,那么肯定不存在這個(gè)表,所以用個(gè)try……except。

i.coordinate用來(lái)獲取一個(gè)格子的坐標(biāo)的。比如輸出結(jié)果就是B11這樣。

try:
sheet=wb[name+'_'+ip]#調(diào)用自己的那一張表
#先給逼刪了
column_B=sheet['B']
foriincolumn_B:
#print(i.value)
ifi.valuenotin('Link','UP'):
num=re.search('d+',i.coordinate).group()#找到需要?jiǎng)h的那一行
print(num)
sheet.delete_rows(int(num))
except:
passtry:
sheet=wb[name+'_'+ip]#調(diào)用自己的那一張表
#先給逼刪了
column_B=sheet['B']
foriincolumn_B:
#print(i.value)
ifi.valuenotin('Link','UP'):
num=re.search('d+',i.coordinate).group()#找到需要?jiǎng)h的那一行
print(num)
sheet.delete_rows(int(num))
except:
pass
defssh_seesion(ip,ouput,):
globalcontent,sheet
#這幾個(gè)列表是寫入工作表的先行條件,也就是工作表的每一列,先把想寫入工作表的每一列的內(nèi)容寫入列表,然后再遍歷列表把內(nèi)容寫入工作表
interface_list=[]
link_list=[]
speed_list=[]
description_list=[]

connection_info={'device_type':'hp_comware',
'ip':ip,
'username':'xxxxxx',
'password':'123'}

withConnectHandler(**connection_info)asconn:
output=conn.send_command("displayinterfacebrief",use_textfsm=True)
sysname=conn.send_command('displaycurrent-configuration|includesysname')
name=re.search(r's+S+s+(S+)',sysname).groups()[0]
#pprint(output)
然后取出接口為UP的接口的信息,放入表格中,因?yàn)檫@個(gè)工作簿的目的是存接口為UP的接口的信息。
#取出接口up的
foriinoutput:
ifi.get('link')=='UP':
interface_list.append(i.get('interface'))
link_list.append(i.get('link'))
speed_list.append(i.get('speed'))
description_list.append(i.get('description'))
#判斷這次interface跟上次也就是表格里的有沒(méi)有區(qū)別以是否是UP的為前提不是UP的或者多了UP的或者少了UP的只要變化就要被記錄

再然后就是把表中原有的接口記錄在sheet_pre_A1這個(gè)例表中,然后與剛才新構(gòu)成的接口全為UP的列表interface_list取差集,差集包含什么?包含可能有新接口UP了,可能有舊接口不UP了。如果差集中有接口,就寫入content中,為了發(fā)郵箱用。

然后判斷差集里面接口的狀態(tài),是又其他狀態(tài)變?yōu)閁P,還是由UP變?yōu)榱似渌麪顟B(tài)。還是寫如content中,發(fā)郵箱用。

ifstr(name+'_'+ip)inwb.sheetnames:#如果這個(gè)表存在就讓里原本有的接口進(jìn)入列表
sheet_pre=wb[name+'_'+ip]
column_A=sheet_pre['A']#取出以前的表的第一列
sheet_pre_A1=[i.valueforiincolumn_A]
sheet_pre_A1.remove('Interfaces')#遍歷第一列的時(shí)候會(huì)有抬頭也就是Interfaces'需要把這個(gè)去掉
#print(sheet_pre_A1)#sheet_pre_A1里是上一次表格里有的接口列表
#print(interface_list)#interface_list里是這一次想放入表格里的UP的接口的列表
#取出兩個(gè)列表的差集,這個(gè)差集是現(xiàn)在UP的和表里的差集
sheet_dif=list(set(sheet_pre_A1)^(set(interface_list)))#把兩個(gè)表中變化的接口放入sheet_dif這個(gè)列表里
print(sheet_dif)#至此有變化的且不是UP的接口就進(jìn)入列表了
iflen(sheet_dif)!=0:#如果這個(gè)列表里有數(shù)據(jù)就發(fā)郵箱
content=content+f"{name}{str(sheet_dif)}接口發(fā)生了變化
'"#配合發(fā)郵件的

foriinoutput:#為了把差集的接口情況寫入列表
forpinsheet_dif:
ifp==i.get('interface')andi.get('link')!='UP':#找到這個(gè)不是UP的接口各種情況還寫進(jìn)去
interface_list.append(i.get('interface'))
link_list.append(i.get('link'))
speed_list.append(i.get('speed'))
description_list.append(i.get('description'))
content=content+i.get('interface')+'接口由UP變成了'+i.get('link')+'
'
elifp==i.get('interface')andi.get('link')=='UP':
content=content+i.get('interface')+'接口UP了'+'
'
第一行加粗并且黃色,有狀態(tài)變化的那一格是綠色。
font=Font(name="微軟雅黑",bold=True)#字體加粗
yellowFill=PatternFill(start_color='FFFF00',end_color='FFFF00',fill_type='solid')#黃色
thin_border=Border(left=Side(style='thin'),right=Side(style='thin'),top=Side(style='thin'),
bottom=Side(style='thin'))#有邊框
SpringGreen=PatternFill(start_color='3CB371',end_color='3CB371',fill_type='solid')#黃色
然后開(kāi)始往工作表里寫東西,如果工作表存在,那么直接寫,相當(dāng)于覆蓋。
寫完之后查看B1這一列,也就是Link這一列,不是UP的給賦值綠色。
ifstr(name+'_'+ip)inwb.sheetnames:#如果表格存在直接往里寫
#在寫之前先刪除#去表格里找,如果檢測(cè)到上次接口不是up則把這個(gè)接口刪掉,不是從python的列表里刪掉,直接從表格里刪掉

row_numbers=list(range(2,len(output)+2))#只能從第二行開(kāi)始
forinterface,rowinzip(interface_list,row_numbers):
sheet.cell(row=row,column=1,value=interface)
forlink,rowinzip(link_list,row_numbers):
sheet.cell(row=row,column=2,value=link)
forspeed,rowinzip(speed_list,row_numbers):
sheet.cell(row=row,column=3,value=speed)
fordescription,rowinzip(description_list,row_numbers):
sheet.cell(row=row,column=4,value=description)
#往里寫完之后查看B1這列然后找到不是UP的給賦值綠色
column_B=sheet['B']
foriincolumn_B:
#print(i.value)
ifi.valuenotin('Link','UP'):
print(i.coordinate)#查找到接口有問(wèn)題的坐標(biāo)
sheet[i.coordinate].fill=SpringGreen
如果表格不存在則創(chuàng)建表格再往里寫,服務(wù)于第一次執(zhí)行程序
else:#如果表格不存在則創(chuàng)建表格
sheet=wb.create_sheet(name+'_'+ip)#這里的sheet相當(dāng)于JT-6-1F-DAS-1這個(gè)表格
columns=['A1','B1','C1','D1']
cells=['Interfaces','Link','Speed','Description']
fori,pinzip(columns,cells):
#放入表格中
sheet[i]=p
sheet[i].fill=yellowFill
sheet[i].font=font

row_numbers=list(range(2,len(output)+2))#只能從第二行開(kāi)始
forinterface,rowinzip(interface_list,row_numbers):
sheet.cell(row=row,column=1,value=interface)
forlink,rowinzip(link_list,row_numbers):
sheet.cell(row=row,column=2,value=link)
forspeed,rowinzip(speed_list,row_numbers):
sheet.cell(row=row,column=3,value=speed)
fordescription,rowinzip(description_list,row_numbers):
sheet.cell(row=row,column=4,value=description)
一段以一列中最寬的一格為標(biāo)準(zhǔn),自動(dòng)變換列寬的代碼
forrowinsheet.rows:
#print(row)
forcellinrow:
#print(cell.value)
cell.border=thin_border
ifcell.value:
dims[cell.column_letter]=max((dims.get(cell.column_letter,0),len(str(cell.value))))

forcol,valueindims.items():
sheet.column_dimensions[col].width=value+3
然后是發(fā)郵件的函數(shù),就不做過(guò)多介紹了
defsend_email(sender,receicer,password,content):

最后執(zhí)行主函數(shù)

先是創(chuàng)建工作簿,因?yàn)榭赡芄ぷ鞑疽呀?jīng)存在了,所以用try,然后用了多線程快一點(diǎn),再然后判斷content里是否有內(nèi)容,只要接口發(fā)生了狀態(tài)變化content中就有變化,content有變化就發(fā)郵件,郵件附件是工作簿。

print(f"程序于{time.strftime('%X')}執(zhí)行開(kāi)始
")
#記錄開(kāi)始時(shí)間
start_time=time.time()

#注意邏輯關(guān)系先創(chuàng)建工作簿再進(jìn)入多線程最后保存工作簿
try:#如果存在這個(gè)表格就直接打開(kāi),如果部存在就創(chuàng)建
wb=load_workbook('test_openpyxl.xlsx')
ws=wb.active
except:#創(chuàng)建表格如果存在就不創(chuàng)建
wb=Workbook()
wb.remove(wb['Sheet'])
ws=wb.active

foripsinip_list.readlines():
t=threading.Thread(target=ssh_seesion,args=(ips.strip(),Queue()))
t.start()
threads.append(t)


foriinthreads:
i.join()
#加入檢查功能


iflen(content)!=0:
print(content)
send_email("1123824309@qq.com","1123824309@qq.com","jveyorpbogllijhj",content)


end_time=time.time()-start_time

wb.save('test_openpyxl.xlsx')#保存工作表
print(f'總共耗時(shí){round(end_time,2)}秒')
print(f"程序于{time.strftime('%X')}執(zhí)行結(jié)束
")

3.3思路合集

82c9c25e-4f45-11ed-a3b6-dac502259ad0.jpg

四、測(cè)試

首先用四個(gè)設(shè)備做測(cè)試

82d72ca0-4f45-11ed-a3b6-dac502259ad0.jpg

第一次執(zhí)行成功輸出工作簿,下面的工作表示以名字_ip展現(xiàn)

82e908da-4f45-11ed-a3b6-dac502259ad0.jpg

然后然別斷開(kāi)兩個(gè)設(shè)備的兩個(gè)接口之后再執(zhí)行一次程序

8304208e-4f45-11ed-a3b6-dac502259ad0.jpg

831bca4a-4f45-11ed-a3b6-dac502259ad0.jpg

如果此時(shí)你就是想把這個(gè)接口認(rèn)為donw掉,然后再執(zhí)行一次程序,也不會(huì)有郵件發(fā)出,down掉的接口的那一行也被刪除了

8337a878-4f45-11ed-a3b6-dac502259ad0.jpg

如果此時(shí)接口恢復(fù)UP,會(huì)發(fā)郵件通知,而且UP的接口也進(jìn)入到工作表中了。

836b444e-4f45-11ed-a3b6-dac502259ad0.jpg

839fc1d8-4f45-11ed-a3b6-dac502259ad0.jpg

五、總結(jié)

最后把這個(gè)腳本仍在服務(wù)器里,一分鐘執(zhí)行一次,這樣一個(gè)低成本的監(jiān)控交換機(jī)接口狀態(tài)變化的腳本就寫完了,其實(shí)還有點(diǎn)小問(wèn)題,比如果接口狀態(tài)不是up了,在輸出工作表時(shí),不是UP的那一行就變?yōu)榱俗詈笠恍小?/span>

在上大學(xué)有一門課叫軟件工程,我記得老師教的一句話是”程序開(kāi)發(fā)時(shí)要高內(nèi)聚,低耦合。”然后再看一眼我的代碼,真的是有些丑陋,寫程序時(shí)常常思維不清晰,邏輯不準(zhǔn)確。反正,這個(gè)腳本在我們現(xiàn)有的網(wǎng)絡(luò)里能用。

審核編輯 :李倩


聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • ADM
    ADM
    +關(guān)注

    關(guān)注

    0

    文章

    30

    瀏覽量

    16337
  • 弱電
    +關(guān)注

    關(guān)注

    0

    文章

    88

    瀏覽量

    15321
  • python
    +關(guān)注

    關(guān)注

    57

    文章

    4869

    瀏覽量

    89934

原文標(biāo)題:網(wǎng)工Python之路之Netmiko+excle定時(shí)檢測(cè)接口狀態(tài)

文章出處:【微信號(hào):釋然IT雜談,微信公眾號(hào):釋然IT雜談】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。

收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評(píng)論

    相關(guān)推薦
    熱點(diǎn)推薦

    探索MC33972:多開(kāi)關(guān)檢測(cè)接口的卓越之選

    它能為我們的設(shè)計(jì)帶來(lái)哪些驚喜。 文件下載: MC33972ATEWR2.pdf 一、產(chǎn)品概述 MC33972是一款具有抑制喚醒功能的多開(kāi)關(guān)檢測(cè)接口芯片,它能夠檢測(cè)多達(dá)22個(gè)開(kāi)關(guān)觸點(diǎn)的閉合與斷開(kāi)
    的頭像 發(fā)表于 01-31 17:45 ?1070次閱讀

    1688交易API:付款狀態(tài)跟蹤與資金安全技術(shù)解析

    交易API通過(guò)alibaba.trade.get接口提供訂單狀態(tài)查詢能力,關(guān)鍵參數(shù)包括: order_id:訂單唯一標(biāo)識(shí) status:動(dòng)態(tài)狀態(tài)碼(如1=待付款, 2=已付款, 3=已發(fā)貨)
    的頭像 發(fā)表于 01-19 14:54 ?197次閱讀
    1688交易API:付款<b class='flag-5'>狀態(tài)</b>跟蹤與資金安全技術(shù)解析

    電容觸摸檢測(cè)電路軟件的過(guò)程

    在范例程序中,軟件定時(shí)(用定時(shí)器中斷實(shí)現(xiàn))對(duì)每個(gè)被測(cè) IO 充電并檢測(cè)放電時(shí)間 N 次,N 次循環(huán)檢測(cè)后, 將統(tǒng)計(jì)結(jié)果提交濾波器狀態(tài)機(jī),得到
    發(fā)表于 01-07 06:16

    煤炭傳送帶狀態(tài)檢測(cè)系統(tǒng) 基于YOLOv8與邊緣計(jì)算

    煤炭傳送帶狀態(tài)檢測(cè)系統(tǒng)通過(guò)機(jī)器視覺(jué)+邊緣分析技術(shù)對(duì)煤炭皮帶狀況進(jìn)行實(shí)時(shí)監(jiān)測(cè),一旦煤炭傳送帶狀態(tài)檢測(cè)系統(tǒng)監(jiān)測(cè)到皮帶跑偏、堆煤、撕裂、異物等其他情況,煤炭傳送帶
    的頭像 發(fā)表于 12-19 20:17 ?382次閱讀
    煤炭傳送帶<b class='flag-5'>狀態(tài)</b><b class='flag-5'>檢測(cè)</b>系統(tǒng) 基于YOLOv8與邊緣計(jì)算

    適用于汽車系統(tǒng)的TIC12400-Q1 24路輸入多開(kāi)關(guān)檢測(cè)接口解析

    適用于汽車系統(tǒng)的TIC12400-Q1 24路輸入多開(kāi)關(guān)檢測(cè)接口解析 在汽車電子系統(tǒng)中,準(zhǔn)確檢測(cè)外部開(kāi)關(guān)狀態(tài)至關(guān)重要。TIC12400-Q1作為一款先進(jìn)的多開(kāi)關(guān)
    的頭像 發(fā)表于 12-18 16:20 ?234次閱讀

    探索MC33972:多功能開(kāi)關(guān)檢測(cè)接口的卓越之選

    探索MC33972:多功能開(kāi)關(guān)檢測(cè)接口的卓越之選 在電子工程師的日常工作中,選擇一款性能優(yōu)良、功能豐富的開(kāi)關(guān)檢測(cè)接口芯片至關(guān)重要。Freescale Semiconductor的MC3
    的頭像 發(fā)表于 12-16 17:50 ?580次閱讀

    信道活躍檢測(cè)(CAD)是一種無(wú)線喚醒技術(shù),這個(gè)功能一般是定時(shí)檢測(cè)么?還是一直檢測(cè)?

    信道活躍檢測(cè)(CAD)是一種無(wú)線喚醒技術(shù),這個(gè)功能一般是定時(shí)檢測(cè)么?還是一直檢測(cè)
    發(fā)表于 12-10 06:53

    CW32時(shí)鐘的穩(wěn)定檢測(cè)

    HSE、LSE、HSIOSC、LSI、PLL 這 5 種時(shí)鐘源都支持時(shí)鐘穩(wěn)定檢測(cè)功能,用戶可通過(guò)對(duì)應(yīng)時(shí)鐘源的穩(wěn)定標(biāo)志位來(lái)確定時(shí)狀態(tài)。時(shí)鐘穩(wěn)定標(biāo)志在關(guān)閉時(shí)鐘源時(shí)由硬件清 0,在時(shí)鐘源啟動(dòng)并穩(wěn)定后由
    發(fā)表于 12-03 06:15

    芯源電容觸摸檢測(cè)電路軟件的過(guò)程

    在電容kit板的案例程序中,軟件定時(shí)(用定時(shí)器中斷實(shí)現(xiàn))對(duì)每個(gè)被測(cè)IO 充電并檢測(cè)放電時(shí)間N 次,N 次循環(huán)檢測(cè)后, 將統(tǒng)計(jì)結(jié)果提交濾波器狀態(tài)
    發(fā)表于 12-02 06:40

    電能質(zhì)量在線監(jiān)測(cè)裝置的自診斷功能對(duì)通信故障的檢測(cè)邏輯是怎樣的?

    電能質(zhì)量在線監(jiān)測(cè)裝置對(duì)通信故障的檢測(cè)邏輯,核心是 **“分層遞進(jìn) + 軟硬協(xié)同”**:從物理接口到傳輸鏈路,再到協(xié)議交互、冗余鏈路,層層校驗(yàn),每個(gè)環(huán)節(jié)都通過(guò) “硬件狀態(tài)采集 + 軟件邏輯判斷
    的頭像 發(fā)表于 11-09 16:49 ?668次閱讀

    資源狀態(tài)感知是如何實(shí)現(xiàn)對(duì)網(wǎng)絡(luò)鏈路狀態(tài)的實(shí)時(shí)感知的?

    資源狀態(tài)感知對(duì)網(wǎng)絡(luò)鏈路狀態(tài)的實(shí)時(shí)監(jiān)測(cè)是通過(guò)硬件底層檢測(cè)、協(xié)議層交互、算法模型分析的多層協(xié)同實(shí)現(xiàn)的,具體技術(shù)路徑如下: 一、硬件層:物理信號(hào)的實(shí)時(shí)捕獲 PHY 芯片的直接感知以太網(wǎng) PHY 芯片(如
    的頭像 發(fā)表于 11-06 14:49 ?648次閱讀

    淘寶買家/賣家訂單列表、訂單詳情、訂單物流 API 接口全攻略

    一、接口概述 淘寶訂單相關(guān) API 接口為開(kāi)發(fā)者提供了獲取訂單數(shù)據(jù)、追蹤物流狀態(tài)的核心能力,支持以下三大功能: 訂單列表查詢:批量獲取指定時(shí)間范圍內(nèi)的訂單信息,支持按
    的頭像 發(fā)表于 11-06 13:46 ?649次閱讀

    訂單實(shí)時(shí)狀態(tài)查詢接口技術(shù)實(shí)現(xiàn)

    、可靠的訂單實(shí)時(shí)狀態(tài)查詢接口,涵蓋接口設(shè)計(jì)、技術(shù)選型、代碼實(shí)現(xiàn)和性能優(yōu)化。我們將使用Python和Flask框架作為示例,確保內(nèi)容真實(shí)可靠,適合開(kāi)發(fā)人員參考。 1. 接口設(shè)計(jì)原則 訂單
    的頭像 發(fā)表于 10-21 17:58 ?695次閱讀
    訂單實(shí)時(shí)<b class='flag-5'>狀態(tài)</b>查詢<b class='flag-5'>接口</b>技術(shù)實(shí)現(xiàn)

    圣邦微電子車規(guī)級(jí)多路開(kāi)關(guān)檢測(cè)接口SGMCD1020Q介紹

    圣邦微電子推出車規(guī)級(jí)多路開(kāi)關(guān)檢測(cè)接口(MSDI)芯片 SGMCD1020Q,為車身控制模塊帶來(lái)全新解決方案。該芯片具備卓越的多通道檢測(cè)能力,可精確監(jiān)測(cè)多達(dá) 22 個(gè)通道的開(kāi)關(guān)狀態(tài),涵蓋
    的頭像 發(fā)表于 04-10 10:22 ?2417次閱讀
    圣邦微電子車規(guī)級(jí)多路開(kāi)關(guān)<b class='flag-5'>檢測(cè)</b><b class='flag-5'>接口</b>SGMCD1020Q介紹

    STM32G4 CAN為什么無(wú)法檢測(cè)到bus-off狀態(tài)?

    我將STM32G473的板子配置了CAN,并周期發(fā)送了報(bào)文,使用USBCANFD-200U進(jìn)行了接收。 1、起先在CAN盒子的接口處直接對(duì)總線CAN-H和CAN-L進(jìn)行短接,發(fā)現(xiàn)無(wú)法進(jìn)入
    發(fā)表于 03-11 07:46