我正在使用Python2.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'),_BindValue().setComponents(ObjectSyntax().setComponents(None,ApplicationSyntax().setComponents(None,None,TimeTicks(86300650))),VarBind().setComponents(ObjectName('1.3.6.1.6.3.1.1.4.1.0'),_BindValue().setComponents(ObjectSyntax().setComponents(SimpleSyntax().setComponents(None,None,ObjectIdentifier('1.3.6.1.4.1.1182.7386.1.1'))))、VarBind().setComponents(ObjectName('1.3.6.1.4.1.1182.7386.1.1.1.0')、setComponents(ObjectSyntax().setComponents(SimpleSyntax().setComponents(None,None,None,ObjectIdentifier('1.3.6.4.1.1182.7386,八位字符串(hexValue='76616c207573622d636f6e6e2d73746174757320300a')))
如果我把代码改成
print('  %s = %s' % (oid, val))

我收到:
名称=
名称=
名称=
如何正确获取3个名称和值?

最佳答案

您应该使用.getVarBinds()方法,而不是the latest documentation中指出的.getVarBindList()
或者,您可以通过请求varBinds的.items()来获得名称-值对,例如varBinds.items()
还要注意,通知接收者应用程序有一个higher-level interface

07-28 08:04