def str2nums(self, txt): """ str2nums(obj_id) -> object id Convert Object ID (given as string) into a list of integer sub IDs. """ if not txt: raise error.BadArgument('Empty Object ID') # Convert string into a list and filter out empty members # (leading dot causes this) objid_s = string.split(txt, '.') objid_s = filter(lambda x: len(x), objid_s) # Convert a list of symbols into a list of numbers try: objid_n = map(lambda x: string.atol(x), objid_s) except: raise error.BadArgument('Malformed Object ID: ' + str(txt)) if not len(objid_n): raise error.BadArgument('Empty Object ID: ' + str(txt)) return objid_n
def __init__(self, community='public', version=0): if not community: raise error.BadArgument('Bad community name') if type(version) != types.IntType: raise error.BadArgument('Bad SNMP version: ' + str(version)) self.request_id = long(random.random() * 0x7fffffff) self.version = version self.community = community
def send (self, request=None): """ send([message]) Send SNMP message (the specified one or previously submitted with store() method) to remote SNMP process specified on session object creation. """ # Message must present if not request and not self.request: raise error.BadArgument('Empty SNMP message') # Make sure we are given a message if request: # Store new request self.store (request) # Make sure the connection is established, open it otherwise if not self.socket: self.open() try: self.socket.send(self.request) except socket.error, why: raise error.TransportError('send() error: ' + str(why))
def store (self, request): """ store(message) Store SNMP message for later transmission. """ if not request: raise error.BadArgument('Empty SNMP message') # Otherwise, store the message to be sent self.request = request
def nums2str(self, objid_n): """ nums2str(obj_id) -> object id Convert Object ID (given as a list of integer sub Object IDs) into string representation. """ if not objid_n: raise error.BadArgument('Empty numeric Object ID') # Convert a list of number into a list of symbols try: objid_s = map(lambda x: '.%lu' % x, objid_n) except: raise error.BadArgument('Malformed numeric Object ID: ' + str(objid_n)) # Merge all the list members into a string txt = reduce(lambda x, y: x + y, objid_s) if not len(txt): raise error.BadArgument('Empty numeric Object ID: ' + str(objid_n)) return txt
def __init__ (self, agent, community='public'): # Call message class constructor message.message.__init__ (self, community) # Make sure we get all these arguments if not agent: raise error.BadArgument ('Empty SNMP agent name') # Initialize SNMP session self.agent = agent self.port = 161 self.timeout = 1.0 self.retries = 3 self.iface = None # This is a provision for multisession superclass self.request = None self.response = None # Initialize socket self.socket = None
def encode_bindings(self, encoded_oids, encoded_values): """ encode_bindings(encoded_oids, encoded_values) -> bindings Bind together encoded object IDs and their associated values (lists of strings). """ # Get the number of oids to encode size = len(encoded_oids) # Make sure the list is not empty if not size: raise error.BadArgument('Empty list of encoded Object IDs') # Initialize stuff index = 0 encoded_oid_pairs = '' # Encode encoded objid's and encoded values together while index < size: # Encode and concatinate one oid_pair if encoded_values and encoded_values[index]: # Merge oid with value oid_pairs = encoded_oids[index] + \ encoded_values[index] else: # Merge oid with value oid_pairs = encoded_oids[index] + \ self.encode_null() # Encode merged pairs encoded_oid_pairs = encoded_oid_pairs + \ self.encode_sequence (oid_pairs) # Progress index index = index + 1 # Return encoded bindings return self.encode_sequence(encoded_oid_pairs)