【问题标题】:pysnmp error on specific query特定查询上的 pysnmp 错误
【发布时间】:2016-08-03 17:52:23
【问题描述】:

我一直在尝试实现加载我的设备 MIBS 并遍历所有 OIDS 的代码。在这种情况下,当我尝试为 snmp 1.3.6.1.2.1.11 加载 OID 时,smi 在尝试加载特定 OID 时会引发异常。前一个 OID 工作成功:'.1.3.6.1.2.1.11.29.0' 但这个会生成错误消息 '.1.3.6.1.2.1.11.30.0'

例外是:

文件“/opt/anaconda/lib/python2.7/site-packages/pysnmp/smi/rfc1902.py”,第 859 行,在 resolveWithMib raise SmiError('MIB object %r with type %r failed to cast value %r: %s' % (self.args[0].prettyPrint(), self.__args[0].getMibNode().getSyntax ().__class.名称, self.__args[1], sys.exc_info()[1])) ;SmiError: MIB 对象 'SNMPv2-MIB::snmpEnableAuthenTraps.0' 类型为 'Integer32' 无法转换值 Integer32(808466736): ConstraintsIntersection(ConstraintsIntersection(ConstraintsIntersection(), ValueRangeConstraint(-2147483648, 2147483647)), SingleValueConstraint(1, 2)) 在 Integer32 处失败:“SingleValueConstraint(1, 2) 在:“808466736”处失败

这里是演示错误的示例代码。您将需要修改 DEVICE_IP。假设您正在运行 SNMP v1 并且针对社区 'public.它正在运行 pysnmp 版本 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')

【问题讨论】:

    标签: python snmp pysnmp


    【解决方案1】:

    您的 SNMP 代理以 1.3.6.1.2.1.11.30.0=808466736 响应,而 OID 1.3.6.1.2.1.11.30.0 标识 MIB 对象 snmpEnableAuthenTraps 类型INTEGER,只允许使用两个值:1 和 2。

    这是来自 SNMPv2-MIB 的正式定义:

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

    所以这次 pysnmp 似乎做了正确的事——它保护你免受毫无意义的价值的影响。此问题的根本原因是 SNMP 代理为 MIB 对象发送格式错误的值。

    【讨论】:

      猜你喜欢
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      • 2016-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多