我可以在这里看到针对SNMP v1和V2的Trap reciver: http://pysnmp.sourceforge.net/examples/v1arch/asyncore/manager/ntfrcv/transport-tweaks.html
它不支持SNMP v3陷阱。
PYSNMP中有v3陷阱接收器吗?
还有什么东西可以告知收件人吗?
答案 0 :(得分:2)
是的,这是一个SNMPv3 notification receiver示例。相同的代码也适用于INFORM。实际上,相同的代码支持SNMPv1和v2c TRAP / INFORM。
更新:
SNMPv1 / v2c TRAP接收器有义务检查传入消息中的SNMP社区名称(一种非常轻的安全措施)。这就是您需要在接收端将SNMP社区名称配置为SNMP引擎的原因。
如果您需要有关SNMP引擎操作的更多详细信息(如对等网络地址),可以在pysnmp内的战略位置放置一组回调,您可以收听这些回调以收集有关当前正在运行的请求的信息。这是an example。也可以使用{{1}}调用,但现在它被认为已过时。
您可以通过向demo.snmplabs.com(端口162)发送INFORM来试验它。
答案 1 :(得分:1)
感谢@llya Etingof的GitHub solution。
示例:
"""
Multiple SNMP USM users
+++++++++++++++++++++++
Receive SNMP TRAP/INFORM messages with the following options:
* SNMPv1/SNMPv2c
* with SNMP community "public"
* over IPv4/UDP, listening at 127.0.0.1:162
* SNMPv3
* with USM users:
* 'usr-md5-des', auth: MD5, priv DES, ContextEngineId: 8000000001020304
* 'usr-md5-none', auth: MD5, priv NONE, ContextEngineId: 8000000001020304
* 'usr-sha-aes128', auth: SHA, priv AES, ContextEngineId: 8000000001020304
* over IPv4/UDP, listening at 127.0.0.1:162
* print received data on stdout
Either of the following Net-SNMP commands will send notifications to this
receiver:
| $ snmptrap -v2c -c public 127.0.0.1:162 123 1.3.6.1.6.3.1.1.5.1 1.3.6.1.2.1.1.5.0 s test
| $ snmptrap -v3 -u usr-md5-des -l authPriv -A authkey1 -X privkey1 -e 8000000001020304 127.0.0.1 123 1.3.6.1.6.3.1.1.5.1
| $ snmptrap -v3 -u usr-md5-none -l authNoPriv -A authkey1 -e 8000000001020304 127.0.0.1 123 1.3.6.1.6.3.1.1.5.1
| $ snmpinform -v3 -u usr-sha-aes128 -l authPriv -a SHA -A authkey1 -x AES -X privkey1 127.0.0.1 123 1.3.6.1.6.3.1.1.5.1
"""#
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
# Create SNMP engine with autogenernated engineID and pre-bound
# to socket transport dispatcher
snmpEngine = engine.SnmpEngine()
# Transport setup
# UDP over IPv4
config.addTransport(
snmpEngine,
udp.domainName,
udp.UdpTransport().openServerMode(('127.0.0.1', 162))
)
# SNMPv1/2c setup
config.addV1System(snmpEngine, 'my-area', 'public')
# SNMPv3/USM setup
# user: usr-md5-des, auth: MD5, priv DES
config.addV3User(
snmpEngine, 'usr-md5-des',
config.usmHMACMD5AuthProtocol, 'authkey1',
config.usmDESPrivProtocol, 'privkey1'
)
# user: usr-md5-des, auth: MD5, priv DES, securityEngineId: 8000000001020304
# this USM entry is used for TRAP receiving purposes
config.addV3User(
snmpEngine, 'usr-md5-des',
config.usmHMACMD5AuthProtocol, 'authkey1',
config.usmDESPrivProtocol, 'privkey1',
securityEngineId=v2c.OctetString(hexValue='8000000001020304')
)
# user: usr-md5-none, auth: MD5, priv NONE
config.addV3User(
snmpEngine, 'usr-md5-none',
config.usmHMACMD5AuthProtocol, 'authkey1'
)
# user: usr-md5-none, auth: MD5, priv NONE, securityEngineId: 8000000001020304
# this USM entry is used for TRAP receiving purposes
config.addV3User(
snmpEngine, 'usr-md5-none',
config.usmHMACMD5AuthProtocol, 'authkey1',
securityEngineId=v2c.OctetString(hexValue='8000000001020304')
)
# user: usr-sha-aes128, auth: SHA, priv AES
config.addV3User(
snmpEngine, 'usr-sha-aes128',
config.usmHMACSHAAuthProtocol, 'authkey1',
config.usmAesCfb128Protocol, 'privkey1'
)
# user: usr-sha-aes128, auth: SHA, priv AES, securityEngineId: 8000000001020304
# this USM entry is used for TRAP receiving purposes
config.addV3User(
snmpEngine, 'usr-sha-aes128',
config.usmHMACSHAAuthProtocol, 'authkey1',
config.usmAesCfb128Protocol, 'privkey1',
securityEngineId=v2c.OctetString(hexValue='8000000001020304')
)
# Callback function for receiving notifications
# noinspection PyUnusedLocal,PyUnusedLocal,PyUnusedLocal
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
命令:
| $ snmptrap -v2c -c public 127.0.0.1:162 123 1.3.6.1.6.3.1.1.5.1 1.3.6.1.2.1.1.5.0 s test
| $ snmptrap -v3 -u usr-md5-des -l authPriv -A authkey1 -X privkey1 -e 8000000001020304 127.0.0.1 123 1.3.6.1.6.3.1.1.5.1
| $ snmptrap -v3 -u usr-md5-none -l authNoPriv -A authkey1 -e 8000000001020304 127.0.0.1 123 1.3.6.1.6.3.1.1.5.1
| $ snmpinform -v3 -u usr-sha-aes128 -l authPriv -a SHA -A authkey1 -x AES -X privkey1 127.0.0.1 123 1.3.6.1.6.3.1.1.5.1
标准输出:
Notification from ContextEngineId "0x80004fb80543794f53335afe60", ContextName ""
1.3.6.1.2.1.1.3.0 = 123
1.3.6.1.6.3.1.1.4.1.0 = 1.3.6.1.6.3.1.1.5.1
1.3.6.1.2.1.1.5.0 = test
Notification from ContextEngineId "0x80001f88809f7b5c26ef1e1c5e00000000", ContextName ""
1.3.6.1.2.1.1.3.0 = 123
1.3.6.1.6.3.1.1.4.1.0 = 1.3.6.1.6.3.1.1.5.1
Notification from ContextEngineId "0x80001f88809f7b5c26ef1e1c5e00000000", ContextName ""
1.3.6.1.2.1.1.3.0 = 123
1.3.6.1.6.3.1.1.4.1.0 = 1.3.6.1.6.3.1.1.5.1
Notification from ContextEngineId "0x80001f88809f7b5c26ef1e1c5e00000000", ContextName ""
1.3.6.1.2.1.1.3.0 = 123
1.3.6.1.6.3.1.1.4.1.0 = 1.3.6.1.6.3.1.1.5.1
答案 2 :(得分:1)
执行时出现以下错误: '#python3 EXAMPLE-above.py'
'#udp.domainName, AttributeError:模块'pysnmp.carrier.asyncore.dgram.udp'没有属性'domainName''
Centos 7
Python 3.6.8
pysnmp 5.0.0
pysmi-0.3.4
pyasn1-0.4.8
net-snmp5.8.1-pre
'#python3 -v -c'import pysnmp'2>&1 | grep pysmi->不产生任何东西
'#python -v -c'import pysnmp'2>&1 | grep pysmi'
'#zipimport:在/usr/lib/python2.7/site-packages/pysmi-0.3.4-py2.7.egg中找到100个名称'
'#python3 -v -c'import pysnmp'2>&1 | grep pyasn1'
'#zipimport:在'/usr/local/lib/python3.6/site-packages/pyasn1-0.4.8-py3.6.egg'中找到77个名称
已修复:git克隆所有依赖项源,解压缩软件包,cd package-source-dir,然后:#python3 setup.py install
这是我的第一个python / snmp项目,因此很可能有更好/更干净的方法来解决。 希望知道正确的方法。
快乐黑客:)