From 15453d56c2f984338ed1fce0bf8b7c2133742f6e Mon Sep 17 00:00:00 2001 From: elie Date: Sun, 21 Jun 2015 13:24:35 +0000 Subject: ObjectIdentity class additionally supports just a MIB module name initializer in which case if resolves into either first or last symbol in given MIB. Another new option is just a MIB symbol initializer without specifying MIB module. --- CHANGES | 6 +++++- TODO | 3 +++ examples/smi/mib-lookup.py | 30 ++++++++++++++++++++++++++ pysnmp/smi/rfc1902.py | 52 +++++++++++++++++++++++++++++++--------------- 4 files changed, 73 insertions(+), 18 deletions(-) diff --git a/CHANGES b/CHANGES index 0fcf308..6187887 100644 --- a/CHANGES +++ b/CHANGES @@ -93,7 +93,11 @@ Revision 4.2.6 - The oneliner's MibVariable MIB lookup subsystem redesigned for more generality to mimic OBJECT-TYPE macro capabilities related to SNMP PDU handling. The two new classed are ObjectIdentity and ObjectType. - This new subsystem is moved from a scope of oneliner to more common + The ObjectIdentity class additionally supports just a MIB module name + initializer in which case if resolves into either first or last symbol + in given MIB. Another option is just a MIB symbol initializer without + specifying MIB module. + This new subsystem is moved from the scope of oneliner to more common pysnmp.smi.rfc1903 scope to more naturally invoke it from whatever part of pysnmp requires MIB services. - MibBuilder now prepends the contents of environment variables it diff --git a/TODO b/TODO index d1f36b9..23393a8 100644 --- a/TODO +++ b/TODO @@ -49,3 +49,6 @@ Sparse notes on major existing problems/plans * Implement TCP transport support * Rework UNIX sockets transport to make it STREAM-type. + +* The get-first/get-last operations of pysnmp.smi.rfc1902.ObjectIdentity() + may need a means to deal only with specific node types. diff --git a/examples/smi/mib-lookup.py b/examples/smi/mib-lookup.py index 1c83afa..a208897 100644 --- a/examples/smi/mib-lookup.py +++ b/examples/smi/mib-lookup.py @@ -32,6 +32,36 @@ mibVar = rfc1902.ObjectIdentity(str(mibVar)).resolveWithMib(mibView) print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) +# Obtain MIB object information by a mix of OID/label parts +mibVar = rfc1902.ObjectIdentity((1,3,6,1,2,'mib-2',1,'sysDescr')).resolveWithMib(mibView) + +print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) + +# Obtain MIB object information by a label +mibVar = rfc1902.ObjectIdentity('iso.org.dod.internet.mgmt.mib-2.system.sysDescr').resolveWithMib(mibView) + +print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) + +# Obtain the first MIB object in given MIB module +mibVar = rfc1902.ObjectIdentity('SNMPv2-MIB').resolveWithMib(mibView) + +print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) + +# Obtain the last MIB object in given MIB module +mibVar = rfc1902.ObjectIdentity('SNMPv2-MIB', last=True).resolveWithMib(mibView) + +print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) + +# Another way to obtain the first (or last) symbol in MIB module +mibVar = rfc1902.ObjectIdentity('SNMPv2-MIB', '').resolveWithMib(mibView) + +print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) + +# Obtain MIB symbol from whatever MIB it is defined at (MIB should be loaded) +mibVar = rfc1902.ObjectIdentity('', 'sysDescr', 0).resolveWithMib(mibView) + +print(mibVar.prettyPrint(), tuple(mibVar), str(mibVar)) + # Create an OID-value pair (called variable-binding in SNMP) varBind = rfc1902.ObjectType( rfc1902.ObjectIdentity('SNMPv2-MIB', 'sysObjectID', 0), '1.3.6.1' diff --git a/pysnmp/smi/rfc1902.py b/pysnmp/smi/rfc1902.py index 8e5f905..4c23c31 100644 --- a/pysnmp/smi/rfc1902.py +++ b/pysnmp/smi/rfc1902.py @@ -113,19 +113,31 @@ class ObjectIdentity: self.__indices = () - if len(self.__args) == 1: # OID or label + if len(self.__args) == 1: # OID or label or MIB module debug.logger & debug.flagMIB and debug.logger('resolving %s as OID or label' % self.__args) try: - self.__oid = rfc1902.ObjectName(self.__args[0]) + # pyasn1 ObjectIdentifier or sequence of ints or string OID + self.__oid = rfc1902.ObjectName(self.__args[0]) # OID except PyAsn1Error: - try: - label = tuple(self.__args[0].split('.')) - except ValueError: - raise SmiError('Bad OID format %r' % (self.__args[0],)) - prefix, label, suffix = mibViewController.getNodeNameByOid( - label - ) - + # sequence of sub-OIDs and labels + if isinstance(self.__args[0], (list, tuple)): + prefix, label, suffix = mibViewController.getNodeName( + self.__args[0] + ) + # string label + elif '.' in self.__args[0]: + prefix, label, suffix = mibViewController.getNodeNameByOid( + tuple(self.__args[0].split('.')) + ) + # MIB module name + else: + modName = self.__args[0] + mibViewController.mibBuilder.loadModules(modName) + if self.__kwargs.get('last'): + prefix, label, suffix = mibViewController.getLastNodeName(modName) + else: + prefix, label, suffix = mibViewController.getFirstNodeName(modName) + if suffix: try: suffix = tuple([ int(x) for x in suffix ]) @@ -175,16 +187,22 @@ class ObjectIdentity: return self elif len(self.__args) > 1: # MIB, symbol[, index, index ...] - self.__modName = self.__args[0] - if self.__args[1]: + # MIB, symbol, index, index + if self.__args[0] and self.__args[1]: + self.__modName = self.__args[0] self.__symName = self.__args[1] - else: - mibViewController.mibBuilder.loadModules(self.__modName) + # MIB, '' + elif self.__args[0]: + mibViewController.mibBuilder.loadModules(self.__args[0]) if self.__kwargs.get('last'): - oid,_,_ = mibViewController.getLastNodeName(self.__modName) + prefix, label, suffix = mibViewController.getLastNodeName(self.__args[0]) else: - oid,_,_ = mibViewController.getFirstNodeName(self.__modName) - _, self.__symName, _ = mibViewController.getNodeLocation(oid) + prefix, label, suffix = mibViewController.getFirstNodeName(self.__args[0]) + self.__modName, self.__symName, _ = mibViewController.getNodeLocation(prefix) + # '', symbol, index, index + else: + prefix, label, suffix = mibViewController.getNodeName(self.__args[1:]) + self.__modName, self.__symName, _ = mibViewController.getNodeLocation(prefix) mibNode, = mibViewController.mibBuilder.importSymbols( self.__modName, self.__symName -- cgit v1.2.1