Python实现数通设备端口使用情况监控实例

作者:guoyh 时间:2021-02-09 23:56:41 

本文实例讲述了Python实现数通设备端口使用情况监控的方法。分享给大家供大家参考。具体如下:

最近因工作需要,上面要求,每天需上报运维的几百数通设备端口使用情况【】,虽然有现成网管监控工具监控设备状态,但做报表,有点不方便,特写了个小脚本。上传的,为半成品可用程序

注:测试运行于ubuntn,需安装snmpwalk工具,

目标数通设备,需做好相关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'
#鎵ц绯荤粺鍛戒护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,'#'))

运行结果:

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程序设计有所帮助。

标签:Python,端口,监控
0
投稿

猜你喜欢

  • Python使用定时调度任务的方式

    2021-03-10 01:40:24
  • laravel学习教程之存取器

    2023-06-07 20:01:12
  • python代码实现小程序登录流程时序总结

    2022-10-16 17:30:14
  • Windows下Python使用Pandas模块操作Excel文件的教程

    2021-01-05 23:07:40
  • 把你的数据库置于版本控制之下

    2009-04-25 10:01:00
  • 用javascript实现Base64编码

    2008-03-04 16:51:00
  • 使用python语言,比较两个字符串是否相同的实例

    2023-08-24 15:01:14
  • Go语言从单体服务到微服务设计方案详解

    2023-09-02 02:45:57
  • 禁用JavaScript脚本来复制网站内容

    2007-02-03 11:30:00
  • laravel多视图共享数据实例代码

    2023-06-19 01:34:14
  • 绿色下划线的简洁CSS导航代码

    2007-09-17 12:51:00
  • opencv对多种颜色小球的形状及位置判断方式

    2022-12-18 22:47:50
  • python 获取毫秒数,计算调用时长的方法

    2021-10-13 02:30:12
  • Python入门教程3. 列表基本操作【定义、运算、常用函数】 <font color=red>原创</font>

    2023-07-15 13:09:19
  • ThinkPHP5中如何使用redis

    2023-06-13 01:09:10
  • 某大型网络公司应聘时的笔试题目附答案

    2023-11-15 02:01:21
  • 实用301转向到另一域名相应页面的asp代码

    2011-04-18 10:42:00
  • 基于Python编写一个宝石消消乐小游戏

    2021-10-25 05:46:06
  • MySQL数据库性能优化之表结构优化

    2012-05-08 07:10:34
  • javascript preload&lazy load

    2023-09-01 06:15:43
  • asp之家 网络编程 m.aspxhome.com