/usr/share/doc/python-pysnmp2/examples/snmpagent.py is in python-pysnmp2 2.0.9-3build1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #!/usr/local/bin/python -O
"""
Receive SNMP messages from remote SNMP managers and echo reply them.
Since MIB parser is not yet implemented in Python, this script takes and
reports Object IDs in dotted numeric representation only.
Written by Ilya Etingof <ilya@glas.net>, 2000-2002
"""
import sys
import getopt
# Import PySNMP modules
from pysnmp import asn1, v1, v2c
from pysnmp import role
# Initialize help messages
options = 'Options:\n'
options = options + ' -p <port> port to listen for requests from managers. Default is 161.'
usage = 'Usage: %s [options] [local-interface] [community]\n' % sys.argv[0]
usage = usage + options
# Initialize defaults
port = 161
iface = ''
community = None
# Parse possible options
try:
(opts, args) = getopt.getopt(sys.argv[1:], 'hp:',\
['port='])
except getopt.error, why:
print 'getopt error: %s\n%s' % (why, usage)
sys.exit(-1)
try:
for opt in opts:
if opt[0] == '-h' or opt[0] == '--help':
print usage
sys.exit(0)
if opt[0] == '-p' or opt[0] == '--port':
port = int(opt[1])
except ValueError, why:
print 'Bad parameter \'%s\' for option %s: %s\n%s' \
% (opt[1], opt[0], why, usage)
sys.exit(-1)
# Parse optional arguments
if len(args) > 0:
iface = args[0]
if len(args) > 1:
community = args[1]
# Create SNMP agent object
server = role.agent([(iface, port)])
# Listen for SNMP messages from remote SNMP managers
while 1:
# Receive a request message
(question, src) = server.receive()
# Decode request of any version
(req, rest) = v2c.decode(question)
# Decode BER encoded Object IDs.
oids = map(lambda x: x[0], map(asn1.OBJECTID().decode, \
req['encoded_oids']))
# Decode BER encoded values associated with Object IDs.
vals = map(lambda x: x[0](), map(asn1.decode, req['encoded_vals']))
# Print it out
print 'SNMP message from: ' + str(src)
print 'Version: ' + str(req['version']+1) + ', type: ' + str(req['tag'])
if req.has_key('non_repeaters'):
print 'Non-repeaters: ' + str(req['non_repeaters'])
if req.has_key('max_repetitions'):
print 'Max repetitions: ' + str(req['max_repetitions'])
for (oid, val) in map(None, oids, vals):
print oid + ' ---> ' + str(val)
# Verify community name if needed
if community is not None and req['community'] != community:
print 'WARNING: UNMATCHED COMMUNITY NAME: ' + str(community)
continue
# Create a SNMP response objects from request object
rsp = req.reply()
# Reply back to manager
server.send(rsp.encode(), src)
|