1"""
2Serve multiple network transports
3+++++++++++++++++++++++++++++++++
4
5Listen and respond to SNMP GET/SET/GETNEXT/GETBULK queries with
6the following options:
7
8* SNMPv2c
9* with SNMP community "public"
10* allow access to SNMPv2-MIB objects (1.3.6.1.2.1)
11* over IPv4/UDP, listening at 127.0.0.1:161 and
12  over IPv6/UDP, listening at [::1]:161
13
14Either of the following Net-SNMP commands will walk this Agent:
15
16| $ snmpwalk -v2c -c public 127.0.0.1 .1.3.6
17| $ snmpwalk -v2c -c public udp6:[::1] .1.3.6
18
19"""#
20from pysnmp.entity import engine, config
21from pysnmp.entity.rfc3413 import cmdrsp, context
22from pysnmp.carrier.asyncore.dgram import udp, udp6
23
24# Create SNMP engine with autogenernated engineID and pre-bound
25# to socket transport dispatcher
26snmpEngine = engine.SnmpEngine()
27
28# Transport setup
29
30# UDP over IPv4 at 127.0.0.1:161
31config.addTransport(
32    snmpEngine,
33    udp.domainName,
34    udp.UdpTransport().openServerMode(('127.0.0.1', 161))
35)
36# UDP over IPv6 at [::1]:161
37config.addTransport(
38    snmpEngine,
39    udp6.domainName,
40    udp6.Udp6Transport().openServerMode(('::1', 161))
41)
42
43# SNMPv2c setup
44
45# SecurityName <-> CommunityName mapping.
46config.addV1System(snmpEngine, 'my-area', 'public')
47
48# Allow full MIB access for this user / securityModels at VACM
49config.addVacmUser(snmpEngine, 2, 'my-area', 'noAuthNoPriv', (1, 3, 6, 1, 2, 1), (1, 3, 6, 1, 2, 1))
50
51# Get default SNMP context this SNMP engine serves
52snmpContext = context.SnmpContext(snmpEngine)
53
54# Register SNMP Applications at the SNMP engine for particular SNMP context
55cmdrsp.GetCommandResponder(snmpEngine, snmpContext)
56cmdrsp.SetCommandResponder(snmpEngine, snmpContext)
57cmdrsp.NextCommandResponder(snmpEngine, snmpContext)
58cmdrsp.BulkCommandResponder(snmpEngine, snmpContext)
59
60# Register an imaginary never-ending job to keep I/O dispatcher running forever
61snmpEngine.transportDispatcher.jobStarted(1)
62
63# Run I/O dispatcher which would receive queries and send responses
64try:
65    snmpEngine.transportDispatcher.runDispatcher()
66except:
67    snmpEngine.transportDispatcher.closeDispatcher()
68    raise
69