Python 2.7 PYSNMP for SNMP v3是否支持陷阱和通知接收器?

Python 2.7 PYSNMP for SNMP v3是否支持陷阱和通知接收器?,python-2.7,snmp,pysnmp,Python 2.7,Snmp,Pysnmp,我可以在这里看到SNMP v1和V2的陷阱接收器: 它不支持SNMP v3陷阱 PYSNMP中是否有v3陷阱接收器的功能 还有什么要通知接收者的吗?是的,这里有一个例子。同样的代码也适用于通知。事实上,相同的代码支持SNMPv1和v2c陷阱/通知 更新: SNMPv1/v2c陷阱接收器必须检查传入消息中的SNMP社区名称(一种非常轻的安全措施)。这就是为什么需要在接收端将SNMP社区名称配置为SNMP引擎的原因 如果您需要有关SNMP引擎操作的更多详细信息(如对等方的网络地址),则在pysnm

我可以在这里看到SNMP v1和V2的陷阱接收器:

它不支持SNMP v3陷阱

PYSNMP中是否有v3陷阱接收器的功能

还有什么要通知接收者的吗?

是的,这里有一个例子。同样的代码也适用于通知。事实上,相同的代码支持SNMPv1和v2c陷阱/通知

更新:

SNMPv1/v2c陷阱接收器必须检查传入消息中的SNMP社区名称(一种非常轻的安全措施)。这就是为什么需要在接收端将SNMP社区名称配置为SNMP引擎的原因

如果您需要有关SNMP引擎操作的更多详细信息(如对等方的网络地址),则在pysnmp中的战略位置会有一个回调集合,您可以侦听该集合以收集有关当前正在运行的请求的信息。给你。也可以使用
getTransportInfo
调用,但现在认为它已经过时了

您可以通过发送通知到(端口162)来尝试它。

谢谢,@llya Etingof在GitHub中

例如:

"""
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


我在执行时遇到以下错误: “#上面的python3示例.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”导入pysnmp“2>&1 | grep pysmi-->不生成任何内容”

“#python-v-c”导入pysnmp“2>&1 | grep pysmi”

“#zipimport:在/usr/lib/python2.7/site packages/pysmi-0.3.4-py2.7.egg中找到了100个名称”

“#python3-v-c”导入pysnmp“2>&1 | grep pyasn1”

“#zipimport:在“/usr/local/lib/python3.6/site packages/pyasn1-0.4.8-py3.6.egg”中找到77个名称”

修复:git克隆所有依赖源代码,解压包,cd包源目录,然后:#python3 setup.py安装

因为这是我的第一个python/snmp项目,所以很可能有更好的/Clear方法来解决这个问题。 如果知道正确的方法,我将不胜感激


Happy hacking:)

谢谢你的提示。这只管用,而不是ContextEngineId。我如何在这里获得代理的IP地址,如transportAddress?现在,我从ContextEngineId“0x80004fb80563696e7363706c703036347cd19c20”,ContextName“这个十六进制字符串是我的管理器的系统名。对于V1和V2,为什么需要这个映射
config.addV1System(snmpEngine,'my area','testing')
V1和V2陷阱接收器不需要知道社区字符串。我使用
print“stateference{}TransportInfo{}获取Ip。格式(stateference,snmpEngine.msgandpudsp.getTransportInfo(stateference))
@user3502325,我在上面的回答中回答了你的问题。