gpt4 book ai didi

python - 使用 PySNMP 作为自有/供应商 MIB 的陷阱接收器

转载 作者:太空宇宙 更新时间:2023-11-03 21:13:32 25 4
gpt4 key购买 nike

我尝试使用 PySNMP 来接收 SNMPv3 陷阱。我找到了这个示例代码:

#!/usr/bin/env /usr/bin/python3

from pysnmp.entity import engine, config
from pysnmp.carrier.asyncore.dgram import udp
from pysnmp.entity.rfc3413 import ntfrcv
from pysnmp.proto.api import v2c
from pysnmp.smi.rfc1902 import ObjectIdentity

snmpEngine = engine.SnmpEngine()

# Transport setup

# UDP over IPv4
config.addTransport(
snmpEngine,
udp.domainName,
udp.UdpTransport().openServerMode(('0.0.0.0', 162)),
)

# SNMPv3/USM setup
config.addV3User(
snmpEngine, '<username>',
config.usmHMACMD5AuthProtocol, '<password>',
config.usmAesCfb128Protocol, '<password>',
securityEngineId=v2c.OctetString(hexValue='<engineid>')
)

def cbFun(snmpEngine, stateReference, contextEngineId, contextName,
varBinds, cbCtx):
print('Notification from ContextEngineId "%s", ContextName "%s"' (contextEngineId.prettyPrint(), contextName.prettyPrint()))
for name, val in varBinds:
print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))

# Register SNMP Application at the SNMP engine
ntfrcv.NotificationReceiver(snmpEngine, cbFun)

snmpEngine.transportDispatcher.jobStarted(1) # this job would never finish

# Run I/O dispatcher which would receive queries and send confirmations
try:
snmpEngine.transportDispatcher.runDispatcher()
except:
snmpEngine.transportDispatcher.closeDispatcher()
raise

这段代码对我有用,但我得到了原始的陷阱。我有一个想要使用的供应商特定 MIB 文件。但我找不到任何有关如何将 mib 绑定(bind)​​到 snmpEngine 的文档。 PySNMP 文档中使用 MIB 的示例仅显示 SNMP GET 操作的用法,不适用于此处。有人以前尝试过这个并可以帮助我吗?

谢谢!

最佳答案

如果您的目标是将收到的原始变量绑定(bind)解析为人类友好的形式,那么您需要 process这些变量通过 MIB 浏览器对象进行绑定(bind)。

你是对的,这与命令生成器在示例中经常执行的操作完全相同。

from pysnmp.smi import builder, view, compiler, rfc1902

# Assemble MIB browser
mibBuilder = builder.MibBuilder()
mibViewController = view.MibViewController(mibBuilder)
compiler.addMibCompiler(
mibBuilder, sources=['file:///usr/share/snmp/mibs',
'http://mibs.snmplabs.com/asn1/@mib@'])

# Pre-load MIB modules that define objects we receive in TRAPs
mibBuilder.loadModules('SNMPv2-MIB', 'SNMP-COMMUNITY-MIB')

# This is what we would get in a TRAP PDU
varBinds = [
('1.3.6.1.2.1.1.3.0', 12345),
('1.3.6.1.6.3.1.1.4.1.0', '1.3.6.1.6.3.1.1.5.2'),
('1.3.6.1.6.3.18.1.3.0', '0.0.0.0'),
('1.3.6.1.6.3.18.1.4.0', ''),
('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'),
('1.3.6.1.2.1.1.1.0', 'my system')
]

# Pass raw var-binds through MIB browser
varBinds = [
rfc1902.ObjectType(rfc1902.ObjectIdentity(x[0]), x[1]).resolveWithMib(mibViewController)
for x in varBinds
]

for varBind in varBinds:
print(varBind.prettyPrint())

关于python - 使用 PySNMP 作为自有/供应商 MIB 的陷阱接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54882149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com