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

时间:2011-09-19 15:37:09

标签: python snmp pysnmp

我写了一个脚本(下面)来测试我在其中一个应用程序中使用pysnmp的能力。我需要能够使用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实例的确切问题;但是,它当然足够好。

2 个答案:

答案 0 :(得分:1)

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

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

有关食谱,请参阅以下示例:

http://pysnmp.sourceforge.net/examples/4.x/v3arch/oneliner/manager/withmib/nextgen.html

答案 1 :(得分:0)

  

这不能回答关于具有符号名称的ObjectName实例的确切问题;但是,这当然足够好了

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

因此,OID的符号表示不是使用ObjectName携带的,但是您可以在MIB处通过OID查找符号名称。

希望我的解释有道理。