import types
from pysnmp import session
class snmpget (session.session):
"""Fetch arbitrary variables from remote SNMP process
"""
def __init__ (self, agent, community):
"""Explicitly call superclass's constructor as it gets
overloaded by this class constructor and pass a few
arguments alone.
"""
session.session.__init__ (self, agent, community)
def run (self, objids):
"""Query SNMP agent for one or more Object ID's. The objid
argument should be a list of strings where each string
represents a Object ID in dotted numbers notation
(e.g. ['.1.3.6.1.4.1.307.3.2.1.1.1.4.1']).
"""
# Convert string type Object ID's into numeric representation
numeric_objids = map (self.str2nums, objids)
# BER encode SNMP Object ID's to query
encoded_objids = map (self.encode_oid, numeric_objids)
# Since we are going to _query_ SNMP agent for Object ID's
# associated value, there will be no variable values passed to
# SNMP agent.
encoded_values = []
# Build a complete SNMP message of type 'GETREQUEST', pass it a list
# of BER encoded Object ID's to query and an empty list of values
# associated with these Object ID's (empty list as there is no point
# to pass any variables values along the SNMP GET request)
question = self.encode_request ('GETREQUEST', encoded_objids,\
encoded_values)
# Try to send SNMP message to SNMP agent and receive a response.
answer = self.send_and_receive (question)
# As we get a response from SNMP agent, try to disassemble SNMP reply
# and extract two lists of BER encoded SNMP Object ID's and
# associated values).
(encoded_objids, encoded_values) = self.decode_response (answer)
# Decode BER encoded Object ID.
objids = map (self.decode_value, encoded_objids)
# Decode BER encoded values associated with Object ID's.
values = map (self.decode_value, encoded_values)
# Return a tuble of two lists holding Object ID's and associated
# values extracted from SNMP agent reply.
return (objids, values)
# Run the module if it's invoked for execution
if __name__ == '__main__':
# The sys module is required for exit() function
import sys
# Make sure we have got enough args
if len (sys.argv) < 4:
# Report a usage error
print 'Usage: %s ' % sys.argv[0]
# Incomplete command line arguments, exiting
sys.exit (1)
# Create an instance of snmpget class
instance = snmpget (sys.argv[1], sys.argv[2])
# Run snmpget against passed Object ID's and expect it to return a list
# of Object ID's and corresponding values as reported by SNMP agent
(objids, values) = instance.run (sys.argv[3:])
# Convert two lists into a list of tuples for easier printing
results = map (None, objids, values)
# Just print them out
for (objid, value) in results:
response = response + objid + ' ---> ' + repr(value)
Need help? Try PySNMP mailing lists.
|