pysnmp.sourceforge.net
HOME QUICK START DOCUMENTATION EXAMPLES DOWNLOAD
DEVELOPMENT | CONTACT |

TRAP/INFORM


WARNING: you are reading historical documentation! Please, refer here.

Notification Receiver Application

Receive SNMP TRAP/INFORM messages with the following options:

Either of the following Net-SNMP's commands will send notifications to this receiver:

$ snmptrap -v3 -u usr-md5-des -l authPriv -A authkey1 -X privkey1
$ 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

This example uses SHA signing and AES encryption. Other hashing/ciphering algorithms could be configured to the addV3User() such as:

This Notification Receiver is capable of handling both types of SNMP Notifications - TRAP and INFORM. In SNMPv3/USM model, confirmed requests (INFORM) are authenticated against Receiver's SNMP EngineId, while unconfirmed messages (TRAP) are authenticated with Originator's SNMP EngineId.

This has an interesting implication - to receive authenticated/encrypted TRAPs Notification Receiver must know TRAP Originator's SNMP Engine ID but it has no chance to discover as there is no handshake happening between the entities. Thus TRAP Originator's SNMP Engine ID must be configured to Notification Receiver's USM users table.

INFORMs, on the other hand, are request/response-style interaction so SNMP Engine ID discovery procedure could be run to let Notification Originator know Notification Receiver's SNMP Engine ID before actual data get transmitted.

from twisted.internet import reactor
from pysnmp.entity import engine, config
from pysnmp.carrier.twisted import dispatch
from pysnmp.carrier.twisted.dgram import udp
from pysnmp.entity.rfc3413 import ntfrcv
from pysnmp.proto import rfc1902

# Create SNMP engine with autogenernated engineID and pre-bound
# to socket transport dispatcher
snmpEngine = engine.SnmpEngine()

# Transport setup

# Instantiate and register Twisted dispatcher at SNMP engine
snmpEngine.registerTransportDispatcher(dispatch.TwistedDispatcher())

# UDP over IPv4
config.addSocketTransport(
    snmpEngine,
    udp.domainName,
    udp.UdpTwistedTransport().openServerMode(('127.0.0.1', 162))
)

# -- begin of 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, contextEngineId: 8000000001020304
# this USM entry is used for TRAP receiving purposes
config.addV3User(
    snmpEngine, 'usr-md5-des',
    config.usmHMACMD5AuthProtocol, 'authkey1',
    config.usmDESPrivProtocol, 'privkey1',
    contextEngineId=rfc1902.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, contextEngineId: 8000000001020304
# this USM entry is used for TRAP receiving purposes
config.addV3User(
    snmpEngine, 'usr-md5-none',
    config.usmHMACMD5AuthProtocol, 'authkey1',
    contextEngineId=rfc1902.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, contextEngineId: 8000000001020304
# this USM entry is used for TRAP receiving purposes
config.addV3User(
    snmpEngine, 'usr-sha-aes128',
    config.usmHMACSHAAuthProtocol, 'authkey1',
    config.usmAesCfb128Protocol, 'privkey1',
    contextEngineId=rfc1902.OctetString(hexValue='8000000001020304')
)

# -- end of SNMPv3/USM setup

# Callback function for receiving notifications
def cbFun(snmpEngine,
          stateReference,
          contextEngineId, contextName,
          varBinds,
          cbCtx):
    print('Notification received, 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)

# Run Twisted main loop
reactor.run()

To download this example as a Python file, please click here.


Need help? Try PySNMP mailing lists or report to library maintainers.
SourceForge Logo