Python實(shí)現(xiàn)數(shù)通設(shè)備端口使用情況監(jiān)控實(shí)例
本文實(shí)例講述了Python實(shí)現(xiàn)數(shù)通設(shè)備端口使用情況監(jiān)控的方法。分享給大家供大家參考。具體如下:
最近因工作需要,上面要求,每天需上報運(yùn)維的幾百數(shù)通設(shè)備端口使用情況【】,雖然有現(xiàn)成網(wǎng)管監(jiān)控工具監(jiān)控設(shè)備狀態(tài),但做報表,有點(diǎn)不方便,特寫了個小腳本。上傳的,為半成品可用程序
注:測試運(yùn)行于ubuntn,需安裝snmpwalk工具,
目標(biāo)數(shù)通設(shè)備,需做好相關(guān)snmp配置
#/usr/bin/python #coding:utf-8 import os,sys import re from pprint import pprint #甯歌鍘傚鏁伴€氳澶嘙IB鍊? MIB = { 'public':{ 'ports':{ 'string':'.1.3.6.1.2.1.2.2.1.2', 'status':'.1.3.6.1.2.1.2.2.1.8', #2 down } }, 'huawei':{ }, 'cisco':{ } } #绔彛鐘舵€佽漿鎹? def portStatus(_s): if int(_s)==2: return 'down' elif int(_s)==1: return 'up' else: return 'none' #鎵ц緋葷粺鍛戒護(hù)snmpwalk璋冪敤 def snmpwalk(host,publicKey,iso): return [i.strip() for i in os.popen('/usr/bin/snmpwalk -c %s -v 2c %s %s'%(publicKey,host,iso)).readlines()] def getPorts(_ip,_public,option): if option == 'ports': postsString = (j.split('=')[1].split(':')[1].replace('"','').strip() for j in snmpwalk(_ip,_public,MIB['public']['ports']['string'])) postsStatus = (int(j.split('=')[1].split(':')[1].strip()) for j in snmpwalk(_ip,_public,MIB['public']['ports']['status'])) return zip(postsString,postsStatus) else: print('on this commmnad') publicKey = 'hi' #sunmp public key HOSTS={ '10.221.98.2':{'type':'switch','origin':'quidway','public':publicKey}, '10.221.98.3':{'type':'switch','origin':'quidway','public':publicKey}, '10.221.97.108':{'type':'firewall','origin':'h3c','public':publicKey}, '10.231.98.233':{'type':'switch','origin':'cisco','public':publicKey}, } if __name__ == '__main__': for i in HOSTS.keys(): for host,status in getPorts(i,HOSTS[i]['public'],'ports'): print("%s\t%s\t%s"%(i,host.ljust(30),portStatus(status).ljust(20))) print(''.ljust(50,'#'))
運(yùn)行結(jié)果:
root@ubuntn12:/tmp# python snmpwalk.py
10.221.98.2 InLoopBack0 up
10.221.98.2 NULL0 up
10.221.98.2 GigabitEthernet0/0/0 down
10.221.98.2 Aux0/0/1 down
10.221.98.2 Vlanif100 up
10.221.98.2 Eth-Trunk1 down
10.221.98.2 Eth-Trunk1.32 down
10.221.98.2 Eth-Trunk1.50 down
10.221.98.2 Eth-Trunk1.60 down
10.221.98.2 Eth-Trunk1.70 down
10.221.98.2 Eth-Trunk1.80 down
10.221.98.2 Eth-Trunk1.100 down
10.221.98.2 Eth-Trunk1.110 down
.......
希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
淺談Pandas dataframe數(shù)據(jù)處理方法的速度比較
這篇文章主要介紹了淺談Pandas dataframe數(shù)據(jù)處理方法的速度比較,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04使用Python的Flask框架來搭建第一個Web應(yīng)用程序
Flask框架是一個以輕量級著稱的Web開發(fā)框架,近兩年來在Web領(lǐng)域獲得了極高的人氣,這里我們就來看如何使用Python的Flask框架來搭建第一個Web應(yīng)用程序2016-06-06caffe的python接口deploy生成caffemodel分類新的圖片
這篇文章主要為大家介紹了caffe的python接口生成deploy文件學(xué)習(xí)以及用訓(xùn)練好的模型(caffemodel)來分類新的圖片示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06詳解python statistics模塊及函數(shù)用法
本節(jié)介紹 Python 中的另一個常用模塊 —— statistics模塊,該模塊提供了用于計(jì)算數(shù)字?jǐn)?shù)據(jù)的數(shù)理統(tǒng)計(jì)量的函數(shù)。這篇文章重點(diǎn)給大家介紹python statistics 模塊的一些用法,感興趣的朋友跟隨小編一起看看吧2019-10-10python如何通過pyqt5實(shí)現(xiàn)進(jìn)度條
這篇文章主要介紹了python如何通過pyqt5實(shí)現(xiàn)進(jìn)度條,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01python使用裝飾器和線程限制函數(shù)執(zhí)行時間的方法
這篇文章主要介紹了python使用裝飾器和線程限制函數(shù)執(zhí)行時間的方法,主要涉及timelimited函數(shù)的使用技巧,非常具有實(shí)用價值,需要的朋友可以參考下2015-04-04python 發(fā)送和接收ActiveMQ消息的實(shí)例
今天小編就為大家分享一篇python 發(fā)送和接收ActiveMQ消息的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01