如何正确访问pysnmp陷阱中返回的varBinds

时间:2017-08-31 18:10:03

标签: python pysnmp

我使用python 2.7并尝试使用pysnmp捕获SNMP陷阱。我正在使用http://pysnmp.sourceforge.net/examples/current/v1arch/manager/ntfrcv/v2c-multiple-transports.html中的示例。我遇到了从varBinds正确获取键/值的问题。示例代码似乎无法正常工作。
完整代码:

from pysnmp.carrier.asynsock.dispatch import AsynsockDispatcher
from pysnmp.carrier.asynsock.dgram import udp, udp6
from pyasn1.codec.ber import decoder
from pysnmp.proto import api

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:
                j = pMod.apiTrapPDU.getEnterprise(reqPDU)
                print 'Enterprise: ({})'.format(str(j))

                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 List: ({})".format(str(varBinds)))

            print('Var-binds:')

            for oid, val in varBinds:
                #print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))
                print('  %s = %s' % (oid, val))


     return wholeMsg

transportDispatcher = AsynsockDispatcher()

transportDispatcher.registerRecvCbFun(cbFun)

# UDP/IPv4
# ip address changed for public posting
transportDispatcher.registerTransport(
    udp.domainName,     udp.UdpSocketTransport().openServerMode(('255.255.255.255', 162))
)

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

transportDispatcher.jobStarted(1)

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

但是,当它运行代码

时,我确实收到了陷阱
print('Var-binds:')
for oid, val in varBinds:
    print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))

我收到了AttributeError:' str'对象没有属性' prettyPrint'

打印varBinds我得到了这个:
(VarBindList()。setComponents(VarBind()。setComponents(ObjectName(' 1.3.6.1.2.1.1.3.0'),_ OptionValue()。setComponents(ObjectSyntax()。setComponents(None,ApplicationSyntax() .setComponents(None,None,TimeTicks(86300650))))),VarBind()。setComponents(ObjectName(' 1.3.6.1.6.3.1.1.4.1.0'),_ OptionValue()。setComponents( ObjectSyntax()。setComponents(SimpleSyntax()。setComponents(None,None,ObjectIdentifier(' 1.3.6.1.4.1.1182.7386.1.1'))))),VarBind()。setComponents(ObjectName(& #39; 1.3.6.1.4.1.1182.7386.1.1.1.0'),_ OptionValue()。setComponents(ObjectSyntax()。setComponents(SimpleSyntax()。setComponents(None,OctetString(hexValue =' 76616c207573622d636f6e6e2d73746174757320300a&# 39)))))))
如果我将代码更改为

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

我收到:

  

name =
   name =
   name =

如何正确获取3个名称和值?

1 个答案:

答案 0 :(得分:4)

您应该使用.getVarBinds()方法而不是the latest documentation中提到的.getVarBindList()

或者,您可以通过请求varBinds的.items()来获取名称 - 值对,例如varBinds.items()

另请注意,Notification Receiver应用程序有higher-level interface