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

yipeiwu_com5年前Python基础

本文实例讲述了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实现在同一网络中的本地文件共享方法

本文利用Python3启动简单的HTTP服务器,以实现在同一网络中共享本地文件。 启动HTTP服务器 打开终端,转入目标文件所在文件夹,键入以下命令: $ cd /Users/zer...

Python中使用装饰器和元编程实现结构体类实例

Ruby中有一个很方便的Struct类,用来实现结构体。这样就不用费力的去定义一个完整的类来仅仅用作访问属性。 复制代码 代码如下: class Dog < Struct.new(...

你真的了解Python的random模块吗?

random模块 用于生成伪随机数 源码位置: Lib/random.py(看看就好,千万别随便修改) 真正意义上的随机数(或者随机事件)在某次产生过程中是按照实验过程中表现的分布概率...

python机器学习实战之K均值聚类

本文实例为大家分享了python K均值聚类的具体代码,供大家参考,具体内容如下 #-*- coding:utf-8 -*- #!/usr/bin/python '''''...

python实现自动登录

python实现自动登录

利用python,可以实现填充网页表单,从而自动登录WEB门户。 (注意:以下内容只针对python3) 环境准备: (1)安装python (2)安装splinter,下载源码 py...