将 pysnmp 数字 oid 输出转换为符号名称

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

我编写了一个脚本(如下)来测试我在我的一个应用程序中使用

pysnmp
的效果。 我需要能够 mib-walk 具有类似
ifDescr
之类的名称的网络元素,并且我的输出需要引用与我查询的相同名称...

from pysnmp.smi import builder, view, error
from pysnmp.entity.rfc3413.oneliner import cmdgen

cmdGen = cmdgen.CommandGenerator()
mibBuilder = builder.MibBuilder()
mibPath = mibBuilder.getMibPath() + ('/opt/python/Models/Network/MIBs',)
mibBuilder.setMibPath(*mibPath)
mibBuilder.loadModules(
    'RFC-1213',
    )
mibView = view.MibViewController(mibBuilder)
errorIndication, errorStatus, errorIndex, \
                 varBindTable = cmdGen.nextCmd(
    # SNMP v2
    cmdgen.CommunityData('test-agent', 'public'),
    # Transport
    cmdgen.UdpTransportTarget(('192.168.56.131', 161)),
    (('', 'ifDescr'),),
    )

print varBindTable

问题

当我执行此代码时,

pysnmp
从我本地对
ifDescr
的定义中正确识别了
RFC-1213.py
;但是,
pysnmp
不会将 mib OID 转换为下面输出中的
ifDescr
之类的名称...

问题

如何使

pysnmp
ObjectName()
实例输出为
ObjectName(ifDescr.1)
而不是
ObjectName(1.3.6.1.2.1.2.2.1.2.1)

[
[(ObjectName(1.3.6.1.2.1.2.2.1.2.1), OctetString('Unit: 1 Slot: 0 Port: 1 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.2), OctetString('Unit: 1 Slot: 0 Port: 2 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.3), OctetString('Unit: 1 Slot: 0 Port: 3 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.4), OctetString('Unit: 1 Slot: 0 Port: 4 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.5), OctetString('Unit: 1 Slot: 0 Port: 5 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.6), OctetString('Unit: 1 Slot: 0 Port: 6 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.7), OctetString('Unit: 1 Slot: 0 Port: 7 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.8), OctetString('Unit: 1 Slot: 0 Port: 8 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.9), OctetString('Unit: 1 Slot: 0 Port: 9 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.10), OctetString('Unit: 1 Slot: 0 Port: 10 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.11), OctetString('Unit: 1 Slot: 0 Port: 11 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.12), OctetString('Unit: 1 Slot: 0 Port: 12 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.13), OctetString('Unit: 1 Slot: 0 Port: 13 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.14), OctetString('Unit: 1 Slot: 0 Port: 14 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.15), OctetString('Unit: 1 Slot: 0 Port: 15 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.16), OctetString('Unit: 1 Slot: 0 Port: 16 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.17), OctetString('Unit: 1 Slot: 0 Port: 17 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.18), OctetString('Unit: 1 Slot: 0 Port: 18 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.19), OctetString('Unit: 1 Slot: 0 Port: 19 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.20), OctetString('Unit: 1 Slot: 0 Port: 20 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.21), OctetString('Unit: 1 Slot: 0 Port: 21 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.22), OctetString('Unit: 1 Slot: 0 Port: 22 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.23), OctetString('Unit: 1 Slot: 0 Port: 23 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.24), OctetString('Unit: 1 Slot: 0 Port: 24 10G - Level'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.25), OctetString(' CPU Interface for Unit: 0 Slot: 5 Port: 1'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.26), OctetString('Link Aggregate 1'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.27), OctetString('Link Aggregate 2'))],  
[(ObjectName(1.3.6.1.2.1.2.2.1.2.28), OctetString('Link Aggregate 3'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.29), OctetString('Link Aggregate 4'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.30), OctetString('Link Aggregate 5'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.31), OctetString('Link Aggregate 6'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.32), OctetString('Link Aggregate 7'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.33), OctetString('Link Aggregate 8'))],  
[(ObjectName(1.3.6.1.2.1.2.2.1.2.38), OctetString('Vl21'))], 
[(ObjectName(1.3.6.1.2.1.2.2.1.2.39), OctetString('Vl22'))]
]

更新

基于 Popper12 答案的显式解决方案是

import string
from pysnmp.smi import builder, view, error
from pysnmp.entity.rfc3413.oneliner import cmdgen
from collections import namedtuple as NT

cmdGen = cmdgen.CommandGenerator()
mibBuilder = builder.MibBuilder()
mibPath = mibBuilder.getMibPath() + ('/opt/python/Models/Network/MIBs',)
mibBuilder.setMibPath(*mibPath)
mibBuilder.loadModules(
    'RFC-1213',
    )
mibView = view.MibViewController(mibBuilder)
errorIndication, errorStatus, errorIndex, \
                 varBindTable = cmdGen.nextCmd(
    # SNMP v2
    cmdgen.CommunityData('test-agent', 'public'),
    # Transport
    cmdgen.UdpTransportTarget(('192.168.56.131', 161)),
    (('', 'ifDescr'),),
    )

SNMPObject = NT('SNMPObject', ['modName', 'symName', 'index', 'value'])

if errorIndication:
    print errorIndication
else:
    if errorStatus:
        print '%s at %s\n' % (
            errorStatus.prettyPrint(),
            varBindTable[-1][int(errorIndex)-1]
            )
    else:
        for varBindTableRow in varBindTable:
            for oid, val in varBindTableRow:
                (symName, modName), indices = cmdgen.mibvar.oidToMibName(
                    cmdGen.mibViewController, oid
                    )
                val = cmdgen.mibvar.cloneFromMibValue(
                              cmdGen.mibViewController, modName, symName, val
                      )
                index = int(string.join(map(lambda v: v.prettyPrint(), indices), '.'))
                value = val.prettyPrint()
                print SNMPObject._make([modName, symName, index, value])

当我运行它时,我现在得到...

SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=1, value="'Unit: 1 Slot: 0 Port: 1 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=2, value="'Unit: 1 Slot: 0 Port: 2 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=3, value="'Unit: 1 Slot: 0 Port: 3 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=4, value="'Unit: 1 Slot: 0 Port: 4 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=5, value="'Unit: 1 Slot: 0 Port: 5 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=6, value="'Unit: 1 Slot: 0 Port: 6 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=7, value="'Unit: 1 Slot: 0 Port: 7 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=8, value="'Unit: 1 Slot: 0 Port: 8 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=9, value="'Unit: 1 Slot: 0 Port: 9 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=10, value="'Unit: 1 Slot: 0 Port: 10 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=11, value="'Unit: 1 Slot: 0 Port: 11 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=12, value="'Unit: 1 Slot: 0 Port: 12 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=13, value="'Unit: 1 Slot: 0 Port: 13 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=14, value="'Unit: 1 Slot: 0 Port: 14 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=15, value="'Unit: 1 Slot: 0 Port: 15 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=16, value="'Unit: 1 Slot: 0 Port: 16 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=17, value="'Unit: 1 Slot: 0 Port: 17 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=18, value="'Unit: 1 Slot: 0 Port: 18 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=19, value="'Unit: 1 Slot: 0 Port: 19 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=20, value="'Unit: 1 Slot: 0 Port: 20 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=21, value="'Unit: 1 Slot: 0 Port: 21 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=22, value="'Unit: 1 Slot: 0 Port: 22 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=23, value="'Unit: 1 Slot: 0 Port: 23 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=24, value="'Unit: 1 Slot: 0 Port: 24 10G - Level'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=25, value="' CPU Interface for Unit: 0 Slot: 5 Port: 1'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=26, value="'Link Aggregate 1'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=27, value="'Link Aggregate 2'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=28, value="'Link Aggregate 3'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=29, value="'Link Aggregate 4'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=30, value="'Link Aggregate 5'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=31, value="'Link Aggregate 6'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=32, value="'Link Aggregate 7'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=33, value="'Link Aggregate 8'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=34, value="'Link Aggregate 9'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=35, value="'Link Aggregate 10'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=36, value="'Link Aggregate 11'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=37, value="'Link Aggregate 12'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=38, value="'Vl21'")
SNMPObject(modName='RFC1213-MIB', symName='ifDescr', index=39, value="'Vl22'")

这并不能回答我关于具有符号名称的 ObjectName 实例的确切问题;不过,这确实已经足够好了。

python snmp pysnmp
2个回答
2
投票

一般来说,要在 OID 和符号名称之间进行转换,您需要使用 MIB 查看器组件。

当您使用 pysnmp“oneliner”API 时,会为您的输入 OID/符号隐式调用此转换。然而,它需要针对您的输出 OID(也可能针对值)显式执行。

请参阅以下食谱示例:

https://docs.lextudio.com/pysnmp/


0
投票

这并不能回答我关于具有符号名称的 ObjectName 实例的确切问题;不过,这确实已经足够好了

符号名称是 OID 的别名,与 OID 不同,它们不会出现在协议操作中。 OID 别名映射由称为 MIB 的独立专用数据库单独维护。

因此,OID 的符号表示不随 ObjectName 一起携带,但是您可以在 MIB 中通过 OID 查找符号名称。

希望我的解释有意义。

© www.soinside.com 2019 - 2024. All rights reserved.