Question

I'm currently working on a SNMP module in Python 3 (Python 3.1.3) based on PySnmp to easily send GET/WALK SNMP queries from other programs. This is mainly for fun/learning.

When querying an existing OID, I get a tuple such as :

(ObjectName(1.3.6.1.2.1.1.7.0), Integer(72))

which I can read using a "for" construct.

However, when querying a non-existing OID (which is what I do for unit testing), I get :

(ObjectName(2.3.4.5.6.7.8), NoSuchObject('b'''))

How can I differentiate the "normal" case, where the 2nd element of the tuple is an integer/string/(other?) and the "error" case where this 2nd element is a 'NoSuchObject' ?

Was it helpful?

Solution

like here Determine the type of an object?

type(your_tuple[1]) == NoSuchObject

or

isinstance(your_tuple[1], NoSuchObject)

OTHER TIPS

Another approach is to match ASN.1 types of noSuchObject and the value part of SNMP response var-bind tuple:

>>> from pysnmp.proto import rfc1905
>>> rfc1905.noSuchObject.isSameTypeWith(rfc1905.noSuchObject)
True
>>> rfc1905.noSuchObject.isSameTypeWith(rfc1905.noSuchInstance)
False
>>> rfc1905.noSuchObject.isSameTypeWith(varBind[1]) # varBind from SNMP response
False
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top