python实现数通设备端口监控示例

yipeiwu_com5年前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实现简单日志记录库glog的使用

这篇文章主要介绍了python实现简单日志记录库glog的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 一、 glog的简介 g...

使用Python AIML搭建聊天机器人的方法示例

AIML全名为Artificial Intelligence Markup Language(人工智能标记语言),是一种创建自然语言软件代理的XML语言,是由RichardS. Wall...

Python 存储字符串时节省空间的方法

从 Python 3 开始,str 类型代表着 Unicode 字符串。取决于编码的类型,一个 Unicode 字符可能会占 4 个字节,这个有些时候有点浪费内存。 出于内存占用以及性能...

Python3 中作为一等对象的函数解析

Python3 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。 函数能提高应用的模块性,和代码的重复利用率。你已经知道Python提供了许多内建函数,比如pr...

Python是编译运行的验证方法

虽然Python被说成是一种解释型语言,但是实际上,Python源程序要先经过编译,然后才能运行。 与Java语言类似,Python源程序编译之后得到的是字节码,交由Python虚拟机来...