我写了一个脚本(如下)来测试我在一个应用程序中使用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的名称。。。
问题
如何使pysnmpObjectName()实例输出为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实例的确切问题;不过,这当然足够好了。

最佳答案

一般来说,要在oid和符号名之间进行转换,需要使用MIB查看器组件。
当您使用pysnmp“oneliner”API时,将隐式地为您的输入oid/符号调用此转换。但是,它需要显式地为输出oid执行(也可能为值执行)。
有关配方,请参见以下示例:
http://pysnmp.sourceforge.net/examples/4.x/v3arch/oneliner/manager/withmib/nextgen.html

10-06 16:05