国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現數通設備端口使用情況監控實例

2020-01-04 18:03:58
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了Python實現數通設備端口使用情況監控的方法,涉及Python針對設備監控的操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了Python實現數通設備端口使用情況監控的方法。分享給大家供大家參考。具體如下:

最近因工作需要,上面要求,每天需上報運維的幾百數通設備端口使用情況【】,雖然有現成網管監控工具監控設備狀態,但做報表,有點不方便,特寫了個小腳本。上傳的,為半成品可用程序

注:測試運行于ubuntn,需安裝snmpwalk工具,

目標數通設備,需做好相關snmp配置

 

 
  1. #/usr/bin/python 
  2. #coding:utf-8 
  3. import os,sys 
  4. import re 
  5. from pprint import pprint 
  6. #甯歌鍘傚鏁伴€氳澶嘙IB鍊? 
  7. MIB = { 
  8. 'public':{ 
  9. 'ports':{ 
  10. 'string':'.1.3.6.1.2.1.2.2.1.2'
  11. 'status':'.1.3.6.1.2.1.2.2.1.8', #2 down 
  12. }, 
  13. 'huawei':{ 
  14. }, 
  15. 'cisco':{ 
  16. #绔彛鐘舵€佽漿鎹? 
  17. def portStatus(_s): 
  18. if int(_s)==2: 
  19. return 'down' 
  20. elif int(_s)==1: 
  21. return 'up' 
  22. else
  23. return 'none' 
  24. #鎵ц緋葷粺鍛戒護snmpwalk璋冪敤 
  25. def snmpwalk(host,publicKey,iso): 
  26. return [i.strip() for i in os.popen('/usr/bin/snmpwalk -c %s -v 2c %s %s'%(publicKey,host,iso)).readlines()] 
  27. def getPorts(_ip,_public,option): 
  28. if option == 'ports'
  29. postsString = (j.split('=')[1].split(':')[1].replace('"','').strip() for j in snmpwalk(_ip,_public,MIB['public']['ports']['string'])) 
  30. postsStatus = (int(j.split('=')[1].split(':')[1].strip()) for j in snmpwalk(_ip,_public,MIB['public']['ports']['status'])) 
  31. return zip(postsString,postsStatus) 
  32. else
  33. print('on this commmnad'
  34. publicKey = 'hi' #sunmp public key 
  35. HOSTS={ 
  36. '10.221.98.2':{'type':'switch','origin':'quidway','public':publicKey}, 
  37. '10.221.98.3':{'type':'switch','origin':'quidway','public':publicKey}, 
  38. '10.221.97.108':{'type':'firewall','origin':'h3c','public':publicKey}, 
  39. '10.231.98.233':{'type':'switch','origin':'cisco','public':publicKey}, 
  40. if __name__ == '__main__'
  41. for i in HOSTS.keys(): 
  42. for host,status in getPorts(i,HOSTS[i]['public'],'ports'): 
  43. print("%s/t%s/t%s"%(i,host.ljust(30),portStatus(status).ljust(20))) 
  44. print(''.ljust(50,'#')) 

運行結果:

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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁陕县| 祁东县| 罗田县| 新源县| 福建省| 土默特右旗| 博白县| 洞头县| 郴州市| 渭南市| 湖口县| 通州市| 德兴市| 松溪县| 庆安县| 黄浦区| 西乡县| 广宁县| 周宁县| 吉木乃县| 邵阳县| 永寿县| 冷水江市| 宜兴市| 商洛市| 康保县| 修武县| 泾川县| 绥化市| 徐闻县| 和平县| 兴文县| 柳州市| 米林县| 房山区| 伊宁县| 江山市| 台湾省| 衡东县| 大英县| 东乌珠穆沁旗|