From 2badd13fbc5a16806170deb432a1aeee03b74905 Mon Sep 17 00:00:00 2001 From: elie Date: Tue, 4 Nov 2014 20:21:48 +0000 Subject: initial support for asyncio network transport added --- .../asyncio/agent/cmdrsp/v3-multiple-users.py | 77 ++++++++++++ examples/v3arch/asyncio/agent/ntforg/inform-v3.py | 117 ++++++++++++++++++ examples/v3arch/asyncio/agent/ntforg/trap-v1.py | 132 +++++++++++++++++++++ .../manager/cmdgen/get-v2c-custom-timeout.py | 95 +++++++++++++++ .../v3arch/asyncio/manager/cmdgen/getbulk-v2c.py | 109 +++++++++++++++++ .../cmdgen/getnext-v2c-from-specific-address.py | 108 +++++++++++++++++ .../manager/ntfrcv/v2c-multiple-interfaces.py | 71 +++++++++++ 7 files changed, 709 insertions(+) create mode 100644 examples/v3arch/asyncio/agent/cmdrsp/v3-multiple-users.py create mode 100644 examples/v3arch/asyncio/agent/ntforg/inform-v3.py create mode 100644 examples/v3arch/asyncio/agent/ntforg/trap-v1.py create mode 100644 examples/v3arch/asyncio/manager/cmdgen/get-v2c-custom-timeout.py create mode 100644 examples/v3arch/asyncio/manager/cmdgen/getbulk-v2c.py create mode 100644 examples/v3arch/asyncio/manager/cmdgen/getnext-v2c-from-specific-address.py create mode 100644 examples/v3arch/asyncio/manager/ntfrcv/v2c-multiple-interfaces.py (limited to 'examples') diff --git a/examples/v3arch/asyncio/agent/cmdrsp/v3-multiple-users.py b/examples/v3arch/asyncio/agent/cmdrsp/v3-multiple-users.py new file mode 100644 index 0000000..b41fc49 --- /dev/null +++ b/examples/v3arch/asyncio/agent/cmdrsp/v3-multiple-users.py @@ -0,0 +1,77 @@ +# +# Command Responder +# +# Listen and respond to SNMP GET/SET/GETNEXT/GETBULK queries with +# the following options: +# +# * SNMPv3 +# * with USM user 'usr-md5-des', auth: MD5, priv DES or +# with USM user 'usr-sha-none', auth: SHA, no privacy +# with USM user 'usr-sha-aes128', auth: SHA, priv AES +# * allow access to SNMPv2-MIB objects (1.3.6.1.2.1) +# * over IPv4/UDP, listening at 127.0.0.1:161 +# * using asyncio network transport (available since Python 3.4) +# +# Either of the following Net-SNMP's commands will walk this Agent: +# +# $ snmpwalk -v3 -u usr-md5-des -l authPriv -A authkey1 -X privkey1 localhost .1.3.6 +# $ snmpwalk -v3 -u usr-sha-none -l authNoPriv -a SHA -A authkey1 localhost .1.3.6 +# $ snmpwalk -v3 -u usr-sha-aes128 -l authPriv -a SHA -A authkey1 -x AES -X privkey1 localhost .1.3.6 +# +from pysnmp.entity import engine, config +from pysnmp.entity.rfc3413 import cmdrsp, context +from pysnmp.carrier.asyncio.dgram import udp +import asyncio + +# Get the event loop for this thread +loop = asyncio.get_event_loop() + +# 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', 161)) +) + +# 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-sha-none, auth: SHA, priv NONE +config.addV3User( + snmpEngine, 'usr-sha-none', + config.usmHMACSHAAuthProtocol, 'authkey1' +) +# user: usr-sha-none, auth: SHA, priv AES +config.addV3User( + snmpEngine, 'usr-sha-aes128', + config.usmHMACSHAAuthProtocol, 'authkey1', + config.usmAesCfb128Protocol, 'privkey1' +) + +# Allow full MIB access for each user at VACM +config.addVacmUser(snmpEngine, 3, 'usr-md5-des', 'authPriv', (1,3,6,1,2,1), (1,3,6,1,2,1)) +config.addVacmUser(snmpEngine, 3, 'usr-sha-none', 'authNoPriv', (1,3,6,1,2,1), (1,3,6,1,2,1)) +config.addVacmUser(snmpEngine, 3, 'usr-sha-aes128', 'authPriv', (1,3,6,1,2,1), (1,3,6,1,2,1)) + +# Get default SNMP context this SNMP engine serves +snmpContext = context.SnmpContext(snmpEngine) + +# Register SNMP Applications at the SNMP engine for particular SNMP context +cmdrsp.GetCommandResponder(snmpEngine, snmpContext) +cmdrsp.SetCommandResponder(snmpEngine, snmpContext) +cmdrsp.NextCommandResponder(snmpEngine, snmpContext) +cmdrsp.BulkCommandResponder(snmpEngine, snmpContext) + +# Run asyncio main loop +loop.run_forever() diff --git a/examples/v3arch/asyncio/agent/ntforg/inform-v3.py b/examples/v3arch/asyncio/agent/ntforg/inform-v3.py new file mode 100644 index 0000000..f74018d --- /dev/null +++ b/examples/v3arch/asyncio/agent/ntforg/inform-v3.py @@ -0,0 +1,117 @@ +# +# Notification Originator +# +# Send SNMP INFORM notification using the following options: +# +# * SNMPv3 +# * with user 'usr-md5-none', auth: MD5, priv NONE +# * over IPv4/UDP +# * using asyncio network transport (available from Python 3.4) +# * to a Manager at 127.0.0.1:162 +# * send INFORM notification +# * with TRAP ID 'warmStart' specified as an OID +# * include managed object information 1.3.6.1.2.1.1.5.0 = 'system name' +# +from pysnmp.entity import engine, config +from pysnmp.entity.rfc3413 import context +from pysnmp.entity.rfc3413.asyncio import ntforg +from pysnmp.carrier.asyncio.dgram import udp +from pysnmp.proto import rfc1902 +import asyncio + +# Get the event loop for this thread +loop = asyncio.get_event_loop() + +# Create SNMP engine instance +snmpEngine = engine.SnmpEngine() + +# SNMPv3/USM setup + +# Add USM user +config.addV3User( + snmpEngine, 'usr-md5-none', + config.usmHMACMD5AuthProtocol, 'authkey1' +) +config.addTargetParams(snmpEngine, 'my-creds', 'usr-md5-none', 'authNoPriv') + +# Transport setup + +# +# Setup transport endpoint and bind it with security settings yielding +# a target name. Since Notifications could be sent to multiple Managers +# at once, more than one target entry may be configured (and tagged). +# +config.addTransport( + snmpEngine, + udp.domainName, + udp.UdpTransport().openClientMode() +) +config.addTargetAddr( + snmpEngine, 'my-nms', + udp.domainName, ('127.0.0.1', 162), + 'my-creds', + tagList='all-my-managers' +) + +# Specify what kind of notification should be sent (TRAP or INFORM), +# to what targets (chosen by tag) and what filter should apply to +# the set of targets (selected by tag) +config.addNotificationTarget( + snmpEngine, 'my-notification', 'my-filter', 'all-my-managers', 'inform' +) + +# Allow NOTIFY access to Agent's MIB by this SNMP model (3), securityLevel +# and SecurityName +config.addContext(snmpEngine, '') +config.addVacmUser(snmpEngine, 3, 'usr-md5-none', 'authNoPriv', (), (), (1,3,6)) + +# Create Notification Originator App instance. +ntfOrg = ntforg.NotificationOriginator() + + # Create default SNMP context where contextEngineId == SnmpEngineId +snmpContext = context.SnmpContext(snmpEngine) + +# Error/confirmation receiver +def cbFun(future): + snmpEngine, errorIndication, errorStatus, errorIndex, varBinds = future.result() + print('Notification status - %s' % ( + errorIndication and errorIndication or 'delivered' + ) + ) + + # This also terminates internal timer + config.delTransport( + snmpEngine, + udp.domainName + ) + + # Finish all scheduled tasks and end the loop + loop.stop() + +# Build and submit notification message to dispatcher +future = ntfOrg.sendVarBinds( + snmpEngine, + # Notification targets + 'my-notification', + # SNMP Context + snmpContext, + # contextName + '', + # notification name (SNMPv2-MIB::coldStart) + (1,3,6,1,6,3,1,1,5,1), + # notification objects instance index + None, + # additional var-binds: ( (oid, value), ... ) + [ ((1,3,6,1,2,1,1,5,0), rfc1902.OctetString('system name')) ] +) + +# Register error/response receiver function on future +future.add_done_callback(cbFun) + +print('Notification is scheduled to be sent') + +# Run asyncio main loop +loop.run_forever() + +# Clear the event loop +loop.close() diff --git a/examples/v3arch/asyncio/agent/ntforg/trap-v1.py b/examples/v3arch/asyncio/agent/ntforg/trap-v1.py new file mode 100644 index 0000000..2dd6f4a --- /dev/null +++ b/examples/v3arch/asyncio/agent/ntforg/trap-v1.py @@ -0,0 +1,132 @@ +# +# Notification Originator +# +# Send SNMP notification using the following options: +# +# * SNMPv1 +# * with community name 'public' +# * over IPv4/UDP +# * using asyncio network transport (available from Python 3.4) +# * to a Manager at 127.0.0.1:162 +# * send TRAP notification +# * with TRAP ID 'coldStart' specified as an OID +# * include managed objects information: +# * overriding Uptime value with 12345 +# * overriding Agent Address with '127.0.0.1' +# * overriding Enterprise OID with 1.3.6.1.4.1.20408.4.1.1.2 +# * include managed object information '1.3.6.1.2.1.1.1.0' = 'my system' +# +from pysnmp.entity import engine, config +from pysnmp.entity.rfc3413 import context +from pysnmp.entity.rfc3413.asyncio import ntforg +from pysnmp.carrier.asyncio.dgram import udp +from pysnmp.proto import rfc1902 +import asyncio + +# Get the event loop for this thread +loop = asyncio.get_event_loop() + +# Create SNMP engine instance +snmpEngine = engine.SnmpEngine() + +# SNMPv1 setup + +# SecurityName <-> CommunityName mapping +config.addV1System(snmpEngine, 'my-area', 'public') + +# Specify security settings per SecurityName (SNMPv1 -> 0) +config.addTargetParams(snmpEngine, 'my-creds', 'my-area', 'noAuthNoPriv', 0) + +# Transport setup +# +# Setup transport endpoint and bind it with security settings yielding +# a target name. Since Notifications could be sent to multiple Managers +# at once, more than one target entry may be configured (and tagged). +# + +# UDP/IPv4 +config.addTransport( + snmpEngine, + udp.domainName, + udp.UdpTransport().openClientMode() +) +config.addTargetAddr( + snmpEngine, 'my-nms-1', + udp.domainName, ('127.0.0.1', 162), + 'my-creds', + tagList='all-my-managers' +) + +# Specify what kind of notification should be sent (TRAP or INFORM), +# to what targets (chosen by tag) and what filter should apply to +# the set of targets (selected by tag) +config.addNotificationTarget( + snmpEngine, 'my-notification', 'my-filter', 'all-my-managers', 'trap' +) + +# Allow NOTIFY access to Agent's MIB by this SNMP model (1), securityLevel +# and SecurityName +config.addContext(snmpEngine, '') +config.addVacmUser(snmpEngine, 1, 'my-area', 'noAuthNoPriv', (), (), (1,3,6)) + +# Create Notification Originator App instance. +ntfOrg = ntforg.NotificationOriginator() + + # Create default SNMP context where contextEngineId == SnmpEngineId +snmpContext = context.SnmpContext(snmpEngine) + +# Prepare notification to be sent returning asyncio future object +future = ntfOrg.sendVarBinds( + snmpEngine, + # Notification targets + 'my-notification', + # SNMP Context + snmpContext, + # contextName + '', + # notification name: Generic Trap #6 (enterpriseSpecific) + # and Specific Trap 432 + '1.3.6.1.4.1.20408.4.1.1.2.0.432', + # notification objects instance index + None, + # additional var-binds holding SNMPv1 TRAP details + [ + # Uptime value with 12345 + (rfc1902.ObjectName('1.3.6.1.2.1.1.3.0'), + rfc1902.TimeTicks(12345)), + # Agent Address with '127.0.0.1' + (rfc1902.ObjectName('1.3.6.1.6.3.18.1.3.0'), + rfc1902.IpAddress('127.0.0.1')), + # Enterprise OID with 1.3.6.1.4.1.20408.4.1.1.2 + (rfc1902.ObjectName('1.3.6.1.6.3.1.1.4.3.0'), + rfc1902.ObjectName('1.3.6.1.4.1.20408.4.1.1.2')), + # managed object '1.3.6.1.2.1.1.1.0' = 'my system' + (rfc1902.ObjectName('1.3.6.1.2.1.1.1.0'), + rfc1902.OctetString('my system')) + ] +) + +print('Notification is scheduled to be sent') + +@asyncio.coroutine +def wait(future): + # a hack: wait for outgoing packet to leave us + yield from asyncio.sleep(1) + future.set_result(True) + +asyncio.async(wait(future)) + +# Run asyncio main loop +loop.run_until_complete(future) + +# This also terminates internal timer +config.delTransport( + snmpEngine, + udp.domainName +) + +# Finish all scheduled tasks and end the loop +loop.stop() + +# Clear the event loop +loop.close() diff --git a/examples/v3arch/asyncio/manager/cmdgen/get-v2c-custom-timeout.py b/examples/v3arch/asyncio/manager/cmdgen/get-v2c-custom-timeout.py new file mode 100644 index 0000000..61cefed --- /dev/null +++ b/examples/v3arch/asyncio/manager/cmdgen/get-v2c-custom-timeout.py @@ -0,0 +1,95 @@ +# +# GET Command Generator +# +# Send a SNMP GET request +# with SNMPv2c, community 'public' +# using Asyncio framework for network transport +# over IPv4/UDP +# to an Agent at 195.218.195.228:161 +# wait 3 seconds for response, retry 5 times (plus one initial attempt) +# for an OID in tuple form +# +# This script performs similar to the following Net-SNMP command: +# +# $ snmpget -v2c -c public -ObentU -r 5 -t 1 195.218.195.228 1.3.6.1.2.1.1.1.0 +# +from pysnmp.carrier.asyncio.dgram import udp +from pysnmp.entity.rfc3413.asyncio import cmdgen +from pysnmp.entity import engine, config +import asyncio + +# Get the event loop for this thread +loop = asyncio.get_event_loop() + +# Create SNMP engine instance +snmpEngine = engine.SnmpEngine() + +# +# SNMPv2c setup +# + +# SecurityName <-> CommunityName mapping +config.addV1System(snmpEngine, 'my-area', 'public') + +# Specify security settings per SecurityName (SNMPv1 - 0, SNMPv2c - 1) +config.addTargetParams(snmpEngine, 'my-creds', 'my-area', 'noAuthNoPriv', 1) + +# +# Setup transport endpoint and bind it with security settings yielding +# a target name +# + +# UDP/IPv4 +config.addTransport( + snmpEngine, + udp.domainName, + udp.UdpTransport().openClientMode() +) +config.addTargetAddr( + snmpEngine, 'my-router', + udp.domainName, ('195.218.195.228', 161), + 'my-creds', + timeout=300, # in 1/100 sec + retryCount=5 +) + +# Error/response receiver +def cbFun(future): + (snmpEngine, errorIndication, errorStatus, errorIndex, varBinds) = future.result() + if errorIndication: + print(errorIndication) + elif errorStatus: + print('%s at %s' % ( + errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex)-1][0] or '?' + ) + ) + else: + for oid, val in varBinds: + print('%s = %s' % (oid.prettyPrint(), val.prettyPrint())) + + # This also terminates internal timer + config.delTransport( + snmpEngine, + udp.domainName + ) + + # Finish all scheduled tasks and end the loop + loop.stop() + +# Prepare request to be sent, receive an asyncio future object +future = cmdgen.GetCommandGenerator().sendVarBinds( + snmpEngine, + 'my-router', + None, '', # contextEngineId, contextName + ( ('1.3.6.1.2.1.1.1.0', None), ), +) + +# Register error/response receiver function on future +future.add_done_callback(cbFun) + +# Run asyncio main loop +loop.run_forever() + +# Clear the event loop +loop.close() diff --git a/examples/v3arch/asyncio/manager/cmdgen/getbulk-v2c.py b/examples/v3arch/asyncio/manager/cmdgen/getbulk-v2c.py new file mode 100644 index 0000000..0b40bd0 --- /dev/null +++ b/examples/v3arch/asyncio/manager/cmdgen/getbulk-v2c.py @@ -0,0 +1,109 @@ +# +# GETBULK Command Generator +# +# Send a series of SNMP GETBULK requests +# with SNMPv2c, community 'public' +# using Asyncio framework for network transport +# over IPv4/UDP +# to an Agent at 195.218.195.228:161 +# with values non-repeaters = 0, max-repetitions = 25 +# for two OIDs in tuple form +# stop on end-of-mib condition for both OIDs +# +# This script performs similar to the following Net-SNMP command: +# +# $ snmpbulkwalk -v2c -c public -C n0 -C r25 -ObentU 195.218.195.228 1.3.6.1.2.1.1 1.3.6.1.4.1.1 +# +from pysnmp.entity import engine, config +from pysnmp.proto import rfc1905 +from pysnmp.entity.rfc3413.asyncio import cmdgen +from pysnmp.carrier.asyncio.dgram import udp +import asyncio + +# Get the event loop for this thread +loop = asyncio.get_event_loop() + +# Create SNMP engine instance +snmpEngine = engine.SnmpEngine() + +# +# SNMPv2c setup +# + +# SecurityName <-> CommunityName mapping +config.addV1System(snmpEngine, 'my-area', 'public') + +# Specify security settings per SecurityName (SNMPv1 - 0, SNMPv2c - 1) +config.addTargetParams(snmpEngine, 'my-creds', 'my-area', 'noAuthNoPriv', 1) + +# +# Setup transport endpoint and bind it with security settings yielding +# a target name +# + +# UDP/IPv4 +config.addTransport( + snmpEngine, + udp.domainName, + udp.UdpTransport().openClientMode() +) +config.addTargetAddr( + snmpEngine, 'my-router', + udp.domainName, ('195.218.195.228', 161), + 'my-creds' +) + +# Error/response receiver +def cbFun(cbCtx): + (snmpEngine, errorIndication, + errorStatus, errorIndex, varBindTable, status) = cbCtx.result() + if errorIndication: + print(errorIndication) + elif errorStatus: + print('%s at %s' % ( + errorStatus.prettyPrint(), + errorIndex and varBindTable[-1][int(errorIndex)-1][0] or '?' + ) + ) + else: + for varBindRow in varBindTable: + for oid, val in varBindRow: + print('%s = %s' % (oid.prettyPrint(), val.prettyPrint())) + + # Stop mainloop when we are done walking (optional) + for oid, val in varBindRow: + if not val.isSameTypeWith(rfc1905.endOfMibView): + # Re-create future for next GETNEXT iteration + future = asyncio.Future() + future.add_done_callback(cbFun) + + # This also indicates that we wish to continue walking + status['future'] = future + return + + # This also terminates internal timer + config.delTransport( + snmpEngine, + udp.domainName + ) + + # Stop mainloop on SNMP error (optional) + loop.stop() + +# Prepare request to be sent, receive an asyncio future object +future = cmdgen.BulkCommandGenerator().sendVarBinds( + snmpEngine, + 'my-router', + None, '', # contextEngineId, contextName + 0, 25, # non-repeaters, max-repetitions + ( ('1.3.6.1.2.1.1', None), ('1.3.6.1.4.1.1', None) ) +) + +# Register error/response receiver function on future +future.add_done_callback(cbFun) + +# Run asyncio main loop +loop.run_forever() + +# Clear the event loop +loop.close() diff --git a/examples/v3arch/asyncio/manager/cmdgen/getnext-v2c-from-specific-address.py b/examples/v3arch/asyncio/manager/cmdgen/getnext-v2c-from-specific-address.py new file mode 100644 index 0000000..792487c --- /dev/null +++ b/examples/v3arch/asyncio/manager/cmdgen/getnext-v2c-from-specific-address.py @@ -0,0 +1,108 @@ +# +# GETNEXT Command Generator +# +# Send a series of SNMP GETNEXT requests +# with SNMPv2c, community 'public' +# using Asyncio framework for network transport +# over IPv4/UDP +# to an Agent at 195.218.195.228:161 +# sending packets from any local interface (0.0.0.0), local port 61024 +# for two OIDs in tuple form +# stop on end-of-mib condition for both OIDs +# +# This script performs similar to the following Net-SNMP command: +# +# $ snmpwalk -v2c -c public -ObentU 195.218.195.228 1.3.6.1.2.1.1 1.3.6.1.4.1.1 +# +from pysnmp.entity import engine, config +from pysnmp.proto import rfc1905 +from pysnmp.entity.rfc3413.asyncio import cmdgen +from pysnmp.carrier.asyncio.dgram import udp +import asyncio + +# Get the event loop for this thread +loop = asyncio.get_event_loop() + +# Create SNMP engine instance +snmpEngine = engine.SnmpEngine() + +# +# SNMPv2c setup +# + +# SecurityName <-> CommunityName mapping +config.addV1System(snmpEngine, 'my-area', 'public') + +# Specify security settings per SecurityName (SNMPv1 - 0, SNMPv2c - 1) +config.addTargetParams(snmpEngine, 'my-creds', 'my-area', 'noAuthNoPriv', 1) + +# +# Setup transport endpoint and bind it with security settings yielding +# a target name +# + +# UDP/IPv4 +config.addTransport( + snmpEngine, + udp.domainName, + udp.UdpTransport().openClientMode(('0.0.0.0', 61024)) +) +config.addTargetAddr( + snmpEngine, 'my-router', + udp.domainName, ('195.218.195.228', 161), + 'my-creds' +) + +# Error/response receiver +def cbFun(future): + (snmpEngine, errorIndication, + errorStatus, errorIndex, varBindTable, status) = future.result() + if errorIndication: + print(errorIndication) + elif errorStatus: + print('%s at %s' % ( + errorStatus.prettyPrint(), + errorIndex and varBindTable[-1][int(errorIndex)-1][0] or '?' + ) + ) + else: + for varBindRow in varBindTable: + for oid, val in varBindRow: + print('%s = %s' % (oid.prettyPrint(), val.prettyPrint())) + + # Stop mainloop when we are done walking (optional) + for oid, val in varBindRow: + if not val.isSameTypeWith(rfc1905.endOfMibView): + # Re-create future for next GETNEXT iteration + future = asyncio.Future() + future.add_done_callback(cbFun) + + # This also indicates that we wish to continue walking + status['future'] = future + return + + # This also terminates internal timer + config.delTransport( + snmpEngine, + udp.domainName + ) + + # Stop mainloop on SNMP error (optional) + loop.stop() + +# Prepare request to be sent, receive an asyncio future object +future = cmdgen.NextCommandGenerator().sendVarBinds( + snmpEngine, + 'my-router', + None, '', # contextEngineId, contextName + ( ('1.3.6.1.2.1.1', None), ('1.3.6.1.2.1.11', None) ) +) + +# Register error/response receiver function on future +future.add_done_callback(cbFun) + +# Run asyncio main loop +loop.run_forever() + +# Clear the event loop +loop.close() diff --git a/examples/v3arch/asyncio/manager/ntfrcv/v2c-multiple-interfaces.py b/examples/v3arch/asyncio/manager/ntfrcv/v2c-multiple-interfaces.py new file mode 100644 index 0000000..e84edc5 --- /dev/null +++ b/examples/v3arch/asyncio/manager/ntfrcv/v2c-multiple-interfaces.py @@ -0,0 +1,71 @@ +# +# Notification Receiver +# +# 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 +# over IPv4/UDP, listening at 127.0.0.1:2162 +# * using Asyncio framework for network transport +# * print received data on stdout +# +# Either of the following Net-SNMP's 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 +# $ snmpinform -v2c -c public 127.0.0.1:2162 123 1.3.6.1.6.3.1.1.5.1 +# +from pysnmp.entity import engine, config +from pysnmp.carrier.asyncio.dgram import udp +from pysnmp.entity.rfc3413 import ntfrcv +import asyncio + +# Get the event loop for this thread +loop = asyncio.get_event_loop() + +# Create SNMP engine with autogenernated engineID and pre-bound +# to socket transport dispatcher +snmpEngine = engine.SnmpEngine() + +# Transport setup + +# UDP over IPv4, first listening interface/port +config.addTransport( + snmpEngine, + udp.domainName + (1,), + udp.UdpTransport().openServerMode(('127.0.0.1', 162)) +) + +# UDP over IPv4, second listening interface/port +config.addTransport( + snmpEngine, + udp.domainName + (2,), + udp.UdpTransport().openServerMode(('127.0.0.1', 2162)) +) + +# SNMPv1/2c setup + +# SecurityName <-> CommunityName mapping +config.addV1System(snmpEngine, 'my-area', 'public') + +# Callback function for receiving notifications +def cbFun(snmpEngine, + stateReference, + contextEngineId, contextName, + varBinds, + cbCtx): + transportDomain, transportAddress = snmpEngine.msgAndPduDsp.getTransportInfo(stateReference) + print('Notification from %s, SNMP Engine %s, Context %s' % ( + transportAddress, 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 asyncio main loop +loop.run_forever() -- cgit v1.2.1