import string
from pysnmp import session
class snmpset (session.session):
"""Set arbitrary variables at 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, values):
"""Pass 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)
# BER encode the values of MIB variables (assuming they are strings!)
encoded_values = map (self.encode_string, values)
# Build a complete SNMP message of type 'SETREQUEST', pass it lists
# of BER encoded Object ID's and MIB variables' values associated
# with these Object ID's to set at SNMP agent.
question = self.encode_request ('SETREQUEST', 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 tuple 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)
# Initialize lists of passed Object-ID's and values
objids = []
values = []
# Parse Object-ID's and values from command line arguments
for arg in sys.argv[3:]:
# Split token on Object-ID and value
token = string.split(arg, ':')
# Add them to the dictionary
if len(token) == 2:
# Add Object-ID and value into two lists
objids.append (token[0])
values.append (token[1])
else:
# Report a falure
print 'bad argument: ', arg
# Bad arguments, exiting
sys.exit (1)
# Create an instance of snmpset class
instance = snmpset (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 (objids, values)
# 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.
|