python實(shí)現(xiàn)數(shù)通設(shè)備端口監(jiān)控示例
最近因工作需要,上面要求,每天需上報(bào)運(yùn)維的幾百數(shù)通設(shè)備端口使用情況【】,雖然有現(xiàn)成網(wǎng)管監(jiān)控工具監(jiān)控設(shè)備狀態(tài),但做報(bào)表,有點(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
相關(guān)文章
Python用imghdr模塊識別圖片格式實(shí)例解析
這篇文章主要介紹了Python用imghdr模塊識別圖片格式實(shí)例解析,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
Python代碼統(tǒng)計(jì)耗時(shí)的方法詳解
在現(xiàn)代軟件開發(fā)中,性能優(yōu)化是一個至關(guān)重要的環(huán)節(jié),無論是開發(fā)大型系統(tǒng)還是小型工具,開發(fā)者都需要對代碼的執(zhí)行時(shí)間進(jìn)行精確測量,以便找出瓶頸并優(yōu)化性能,本文給大家介紹了Python代碼統(tǒng)計(jì)耗時(shí)的方法,需要的朋友可以參考下2025-02-02
django第一個項(xiàng)目127.0.0.1:8000不能訪問的解決方案詳析
django項(xiàng)目服務(wù)啟動后無法通過127.0.0.1訪問,下面這篇文章主要給大家介紹了關(guān)于django第一個項(xiàng)目127.0.0.1:8000不能訪問的解決方案,需要的朋友可以參考下2022-10-10
python中with語句結(jié)合上下文管理器操作詳解
這篇文章主要給大家介紹了關(guān)于python中with語句結(jié)合上下文管理器操作的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
詳解Python調(diào)用系統(tǒng)命令的六種方法
這篇文章主要介紹了詳解Python調(diào)用系統(tǒng)命令的六種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Python button選取本地圖片并顯示的實(shí)例
今天小編就為大家分享一篇Python button選取本地圖片并顯示的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
Python 點(diǎn)集排序之帶索引的Z字形排序算法實(shí)現(xiàn)代碼
這篇文章介紹了如何使用Python在Grasshopper中實(shí)現(xiàn)點(diǎn)集排序功能,包括點(diǎn)的Y坐標(biāo)分組和X坐標(biāo)排序,以及追蹤每個點(diǎn)的原始索引位置,通過創(chuàng)建點(diǎn)索引對、分組邏輯和排序,實(shí)現(xiàn)了Z字形排序算法,感興趣的朋友一起看看吧2025-01-01

