如何正确访问 pysnmp 陷阱中返回的 varBind
how to properly access varBinds returned in pysnmp traps
我正在使用 python 2.7 并尝试使用 pysnmp 捕获 SNMP 陷阱。我正在使用 http://pysnmp.sourceforge.net/examples/current/v1arch/manager/ntfrcv/v2c-multiple-transports.html 中的示例。我在从 varBinds 正确获取 key/values 时遇到问题。示例代码似乎无法为此正常工作。
完整代码:
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'), _BindValue().setComponents(ObjectSyntax().setComponents(SimpleSyntax().setComponents(None, OctetString(hexValue='76616c207573622d636f6e6e2d73746174750a')03 ))))))
如果我将代码更改为
print(' %s = %s' % (oid, val))
我收到:
name =
name =
name =
如何正确获取 3 个名称和值?
您应该使用 .getVarBinds()
方法而不是 the latest documentation 中所述的 .getVarBindList()
。
或者,您可以通过请求 varBind 的 .items()
来获取名称-值对,例如varBinds.items()
.
另请注意,通知接收器应用程序有一个 higher-level interface。
我正在使用 python 2.7 并尝试使用 pysnmp 捕获 SNMP 陷阱。我正在使用 http://pysnmp.sourceforge.net/examples/current/v1arch/manager/ntfrcv/v2c-multiple-transports.html 中的示例。我在从 varBinds 正确获取 key/values 时遇到问题。示例代码似乎无法为此正常工作。
完整代码:
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'), _BindValue().setComponents(ObjectSyntax().setComponents(SimpleSyntax().setComponents(None, OctetString(hexValue='76616c207573622d636f6e6e2d73746174750a')03 ))))))
如果我将代码更改为
print(' %s = %s' % (oid, val))
我收到:
name =
name =
name =
如何正确获取 3 个名称和值?
您应该使用 .getVarBinds()
方法而不是 the latest documentation 中所述的 .getVarBindList()
。
或者,您可以通过请求 varBind 的 .items()
来获取名称-值对,例如varBinds.items()
.
另请注意,通知接收器应用程序有一个 higher-level interface。