pysnmp error on specific query
Question: I have been trying to implement code that loads my device MIBS and walks
through all the OIDS. In this one case when I try to load the OID for snmp
1.3.6.1.2.1.11 smi throws an exception when trying to load a specific OID. The
previous OID works successfully: '.1.3.6.1.2.1.11.29.0' but this one generates
the error message '.1.3.6.1.2.1.11.30.0'

The exception is:

> File "/opt/anaconda/lib/python2.7/site-packages/pysnmp/smi/rfc1902.py", line
> 859, in resolveWithMib raise SmiError('MIB object %r having type %r failed
> to cast value %r: %s' % (self.**args[0].prettyPrint(),
> self.__args[0].getMibNode().getSyntax().__class**.**name** , self.__args[1],
> sys.exc_info()[1])) ;SmiError: MIB object
> 'SNMPv2-MIB::snmpEnableAuthenTraps.0' having type 'Integer32' failed to cast
> value Integer32(808466736):
> ConstraintsIntersection(ConstraintsIntersection(ConstraintsIntersection(),
> ValueRangeConstraint(-2147483648, 2147483647)), SingleValueConstraint(1, 2))
> failed at: "SingleValueConstraint(1, 2) failed at: "808466736"" at Integer32

Here is sample code that demonstrates the error. You will need to modify the
DEVICE_IP. It is assuming that you are running SNMP v1 and against community
'public. It is running pysnmp version 4.3.2

    
    
    from pysnmp.entity.rfc3413.oneliner import cmdgen
    from pysnmp.smi.rfc1902 import ObjectIdentity
    
    DEVICE_IP = 'localhost'
    
    
    def get_oid(oid):
        """
        Requires a valid oid as input and retrieves the given OID
        """ 
        snmp_target = (DEVICE_IP, 161)
        cmdGen = cmdgen.CommandGenerator()
        result = None
    
        errorIndication, errorStatus, errorIndex, varBindTable =            cmdGen.nextCmd(
            cmdgen.CommunityData('public', mpModel=0),
            cmdgen.UdpTransportTarget(snmp_target),
            ObjectIdentity(oid, last=True),
            lexicographicMode=False
            )
    
        if errorIndication:
            print(errorIndication)
        else:
            for varBindTableRow in varBindTable:
                for name, val in varBindTableRow:
                    try:
                        result = str(val)
                    except:
                        raise
        return result
    
    # Does not Throw Error
    print get_oid('.1.3.6.1.2.1.11.29.0')
    
    # Throws Error
    print get_oid('.1.3.6.1.2.1.11.30.0')
    


Answer: Your SNMP agent responded with _1.3.6.1.2.1.11.30.0=808466736_ while OID
1.3.6.1.2.1.11.30.0 identifies MIB object _snmpEnableAuthenTraps_ of type
INTEGER with only two values permitted: 1 and 2.

Here is formal definition from SNMPv2-MIB:

    
    
    snmpEnableAuthenTraps OBJECT-TYPE
        SYNTAX      INTEGER { enabled(1), disabled(2) }
        ...
    

So this time pysnmp seems to do the right thing - it shields you from the
value that makes no sense. Root cause of this problem is the SNMP agent that
sends malformed values for MIB objects.

