1"""
2Multiple SNMP USM users
3+++++++++++++++++++++++
4
5Listen and respond to SNMP GET/SET/GETNEXT/GETBULK queries with
6the following options:
7
8* SNMPv3
9* with USM user:
10    - 'usr-md5-des', auth: MD5, priv DES or
11    - 'usr-sha-none', auth: SHA, no privacy
12    - 'usr-sha-aes128', auth: SHA, priv AES
13* allow access to SNMPv2-MIB objects (1.3.6.1.2.1)
14* over IPv4/UDP, listening at 127.0.0.1:161
15* using asyncio network transport (available since Python 3.4)
16
17Either of the following Net-SNMP commands will walk this Agent:
18
19| $ snmpwalk -v3 -u usr-md5-des -l authPriv -A authkey1 -X privkey1 localhost .1.3.6
20| $ snmpwalk -v3 -u usr-sha-none -l authNoPriv -a SHA -A authkey1 localhost .1.3.6
21| $ snmpwalk -v3 -u usr-sha-aes128 -l authPriv -a SHA -A authkey1 -x AES -X privkey1 localhost .1.3.6
22
23Requires Python 3.4 and later!
24
25"""#
26from pysnmp.entity import engine, config
27from pysnmp.entity.rfc3413 import cmdrsp, context
28from pysnmp.carrier.asyncio.dgram import udp
29import asyncio
30
31# Get the event loop for this thread
32loop = asyncio.get_event_loop()
33
34# Create SNMP engine with autogenernated engineID and pre-bound
35# to socket transport dispatcher
36snmpEngine = engine.SnmpEngine()
37
38# Transport setup
39
40# UDP over IPv4
41config.addTransport(
42    snmpEngine,
43    udp.domainName,
44    udp.UdpTransport().openServerMode(('127.0.0.1', 161))
45)
46
47# SNMPv3/USM setup
48
49# user: usr-md5-des, auth: MD5, priv DES
50config.addV3User(
51    snmpEngine, 'usr-md5-des',
52    config.usmHMACMD5AuthProtocol, 'authkey1',
53    config.usmDESPrivProtocol, 'privkey1'
54)
55# user: usr-sha-none, auth: SHA, priv NONE
56config.addV3User(
57    snmpEngine, 'usr-sha-none',
58    config.usmHMACSHAAuthProtocol, 'authkey1'
59)
60# user: usr-sha-none, auth: SHA, priv AES
61config.addV3User(
62    snmpEngine, 'usr-sha-aes128',
63    config.usmHMACSHAAuthProtocol, 'authkey1',
64    config.usmAesCfb128Protocol, 'privkey1'
65)
66
67# Allow full MIB access for each user at VACM
68config.addVacmUser(snmpEngine, 3, 'usr-md5-des', 'authPriv', (1, 3, 6, 1, 2, 1), (1, 3, 6, 1, 2, 1))
69config.addVacmUser(snmpEngine, 3, 'usr-sha-none', 'authNoPriv', (1, 3, 6, 1, 2, 1), (1, 3, 6, 1, 2, 1))
70config.addVacmUser(snmpEngine, 3, 'usr-sha-aes128', 'authPriv', (1, 3, 6, 1, 2, 1), (1, 3, 6, 1, 2, 1))
71
72# Get default SNMP context this SNMP engine serves
73snmpContext = context.SnmpContext(snmpEngine)
74
75# Register SNMP Applications at the SNMP engine for particular SNMP context
76cmdrsp.GetCommandResponder(snmpEngine, snmpContext)
77cmdrsp.SetCommandResponder(snmpEngine, snmpContext)
78cmdrsp.NextCommandResponder(snmpEngine, snmpContext)
79cmdrsp.BulkCommandResponder(snmpEngine, snmpContext)
80
81# Run asyncio main loop
82loop.run_forever()
83