pysnmp 将 oid 解析为 mibname

问题描述 投票:0回答:1

目前我已经设置了trap监听器,它可以很好地监听snmp通知。 然而它只返回数字 oid,我希望能够将此 oid 解析为人类可读的名称。 我已经查看了文档,但我并不太理解它。 我正在使用示例脚本

    from pysnmp.carrier.asyncore.dispatch import AsyncoreDispatcher
from pysnmp.carrier.asyncore.dgram import udp, udp6, unix
from pyasn1.codec.ber import decoder
from pysnmp.proto import api


# noinspection PyUnusedLocal
def cbFun(transportDispatcher, transportDomain, transportAddress, wholeMsg):
    while wholeMsg:
        msgVer = int(api.decodeMessageVersion(wholeMsg))
        if msgVer in api.protoModules:
            pMod = api.protoModules[msgVer]
        else:
            print('Unsupported SNMP version %s' % msgVer)
            return
        reqMsg, wholeMsg = decoder.decode(
            wholeMsg, asn1Spec=pMod.Message(),
        )
        print('Notification message from %s:%s: ' % (
            transportDomain, transportAddress
        )
              )
        reqPDU = pMod.apiMessage.getPDU(reqMsg)
        if reqPDU.isSameTypeWith(pMod.TrapPDU()):
            if msgVer == api.protoVersion1:
                print('Enterprise: %s' % (pMod.apiTrapPDU.getEnterprise(reqPDU).prettyPrint()))
                print('Agent Address: %s' % (pMod.apiTrapPDU.getAgentAddr(reqPDU).prettyPrint()))
                print('Generic Trap: %s' % (pMod.apiTrapPDU.getGenericTrap(reqPDU).prettyPrint()))
                print('Specific Trap: %s' % (pMod.apiTrapPDU.getSpecificTrap(reqPDU).prettyPrint()))
                print('Uptime: %s' % (pMod.apiTrapPDU.getTimeStamp(reqPDU).prettyPrint()))
                varBinds = pMod.apiTrapPDU.getVarBindList(reqPDU)
            else:
                varBinds = pMod.apiPDU.getVarBindList(reqPDU)
            print('Var-binds:')
            for oid, val in varBinds:
                print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))
    return wholeMsg


transportDispatcher = AsyncoreDispatcher()

transportDispatcher.registerRecvCbFun(cbFun)

# UDP/IPv4
transportDispatcher.registerTransport(
    udp.domainName, udp.UdpSocketTransport().openServerMode(('localhost', 162))
)

# UDP/IPv6
transportDispatcher.registerTransport(
    udp6.domainName, udp6.Udp6SocketTransport().openServerMode(('::1', 162))
)

## Local domain socket
# transportDispatcher.registerTransport(
#    unix.domainName, unix.UnixSocketTransport().openServerMode('/tmp/snmp-manager')
# )

transportDispatcher.jobStarted(1)

try:
    # Dispatcher will never finish as job#1 never reaches zero
    transportDispatcher.runDispatcher()
except:
    transportDispatcher.closeDispatcher()
    raise

我想知道如何将 oid 解析为 mib。 我已经下载了 cisco mib .my 文件并将它们粘贴到我指向 mibbuilder 的目录中,

snmpEngine = SnmpEngine()
mibBuilder = builder.MibBuilder()
mibPath = mibBuilder.getMibSources() + (builder.DirMibSource('/opt/mibs'),)
mibBuilder.setMibSources(*mibPath)
mibBuilder.loadModules('CISCO-CONFIG-MAN-MIB',)
mibViewController = view.MibViewController(mibBuilder)

在示例脚本中我执行了 PrettyPrint() 语句

for oid, val in varBinds:
    objectType = ObjectType(ObjectIdentity(oid.prettyPrint()))
    objectType.resolveWithMib(mibViewController)
    print str(objectType)

    print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))

进行更改后,我现在遇到了两个错误,

回溯(最近一次调用最后一次): 文件“traplistener.py”,第 200 行,位于 'CISCO-BRIDGE-EXT-MIB', 文件“/usr/local/lib/python2.7/site-packages/pysnmp/smi/builder.py”,第 344 行,在 loadModules 中 引发错误。MibNotFoundError('%s 编译错误: %s' % (modName, errs)) pysnmp.smi.error.MibNotFoundError:CISCO-CONFIG-MAN-MIB 编译错误:缺失; MIB CISCO-CONFIG-MAN-MIB 的符号表中没有模块“CISCO-SMI”;丢失的;丢失的;丢失的;丢失的;丢失的;失踪了

python python-2.7 snmp pysnmp
1个回答
0
投票

由于您正在使用低级 API(而不是 pysnmp.hlapi),我可以为您提供以下有效包含 MIB 解析器的代码片段:

from pysnmp.smi import builder, view, compiler, rfc1902

# Assemble MIB viewer
mibBuilder = builder.MibBuilder()
compiler.addMibCompiler(mibBuilder, sources=['file:///usr/share/snmp/mibs',
                                             'http://mibs.snmplabs.com/asn1/@mib@'])

mibViewController = view.MibViewController(mibBuilder)

# Pre-load MIB modules we expect to work with
mibBuilder.loadModules('SNMPv2-MIB', 'SNMP-COMMUNITY-MIB')

# This is what we can get in TRAP PDU
varBinds = [
    ('1.3.6.1.2.1.1.3.0', 12345),
    ('1.3.6.1.6.3.1.1.4.1.0', '1.3.6.1.6.3.1.1.5.2'),
    ('1.3.6.1.6.3.18.1.3.0', '0.0.0.0'),
    ('1.3.6.1.6.3.18.1.4.0', ''),
    ('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'),
    ('1.3.6.1.2.1.1.1.0', 'my system')
]

# Run var-binds received in PDU (a sequence of OID-value pairs)
# through MIB viewer to turn them into MIB objects.
# You may want to catch and ignore MIB lookup errors here.
varBinds = [rfc1902.ObjectType(rfc1902.ObjectIdentity(x[0]), x[1]).resolveWithMib(mibViewController) for x in varBinds]

for varBind in varBinds:
    print(varBind.prettyPrint())
© www.soinside.com 2019 - 2024. All rights reserved.