I'm using python 2.7 and trying to capture SNMP traps using pysnmp. I am using the example from http://pysnmp.sourceforge.net/examples/current/v1arch/manager/ntfrcv/v2c-multiple-transports.html. I am having issues with getting the key/values from varBinds properly. The example code doesn't seem to work correctly for this.
Full code:
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
I do receive the trap, however, when it runs the code
print('Var-binds:')
for oid, val in varBinds:
print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))
I receive AttributeError: 'str' object has not attribute 'prettyPrint'
Printing varBinds I get this:
(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'), _BindValue().setComponents(ObjectSyntax().setComponents(SimpleSyntax().setComponents(None, OctetString(hexValue='76616c207573622d636f6e6e2d73746174757320300a')))))))
If I change the code to
print(' %s = %s' % (oid, val))
I receive:
name =
name =
name =
How do I get the 3 names and values properly?