Python 使用PySNMP作为陷阱接收器和自有/供应商MIB
我尝试使用PySNMP来接收SNMPv3陷阱。我发现了以下示例代码:Python 使用PySNMP作为陷阱接收器和自有/供应商MIB,python,pysnmp,mib,Python,Pysnmp,Mib,我尝试使用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.rfc19
#!/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
#/usr/bin/env/usr/bin/python3
从pysnmp.entity导入引擎,配置
从pysnmp.carrier.asyncore.dgram导入udp
从pysnmp.entity.rfc3413导入ntfrcv
从pysnmp.proto.api导入v2c
从pysnmp.smi.rfc1902导入ObjectIdentity
snmpEngine=engine.snmpEngine()
#传输设置
#IPv4上的UDP
config.addTransport(
snmpEngine,
udp.domainName,
udp.udptTransport().openServerMode(('0.0.0.0',162)),
)
#SNMPv3/USM设置
config.addV3User(
snmpEngine,',
config.usmhmadmd5auth协议“”,
config.usmaescfb128协议“”,
securityEngineId=v2c.OctetString(hexValue='')
)
def cbFun(snmpEngine、stateference、contextEngineId、contextName、,
varBinds,cbCtx):
打印('来自ContextEngineId“%s”、ContextName“%s”的通知(ContextEngineId.prettyPrint()、ContextName.prettyPrint())
varBinds中的val作为名称:
打印(“%s=%s%”(name.prettyPrint(),val.prettyPrint())
#在SNMP引擎上注册SNMP应用程序
ntfrcv.通知接收器(snmpEngine、cbFun)
snmpEngine.transportDispatcher.jobStarted(1)#此作业永远不会完成
#运行I/O调度程序,它将接收查询并发送确认
尝试:
snmpEngine.transportDispatcher.runDispatcher()
除:
snmpEngine.transportDispatcher.closeDispatcher()
提升
这个代码对我有用,但我得到了原始的陷阱。我有一个特定于供应商的MIB文件要使用。但是我找不到任何关于如何将mib绑定到snmpEngine的文档。PySNMP文档中使用mib的示例仅显示SNMP GET操作的用法,此处不适用。
以前有人试过这个,能帮我吗
谢谢 如果您的目标是将收到的原始变量绑定解析为人性化的形式,那么您需要通过MIB browser对象访问这些变量绑定 您是对的,这与command generator在示例中经常执行的操作完全相同
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())
谢谢,这就是我问题的解决办法!