PySNMP:加载错误:无效的OctetString初始化器

1 投票
1 回答
793 浏览
提问于 2025-04-17 10:46

我在pysnmp的页面上找到了一个例子:

# GET命令生成器
from pysnmp.entity.rfc3413.oneliner import cmdgen
errorIndication, errorStatus, \
                 errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(
    # SNMP v1
#    cmdgen.CommunityData('test-agent', 'public', 0),
    # SNMP v2
    cmdgen.CommunityData('test-agent', 'public'),
    # SNMP v3
#    cmdgen.UsmUserData('test-user', 'authkey1', 'privkey1'),
    cmdgen.UdpTransportTarget(('localhost', 161)),
    # Plain OID
    (1,3,6,1,2,1,1,1,0),
    # ((mib-name, mib-symbol), instance-id)
    (('SNMPv2-MIB', 'sysDescr'), 0)
    )

if errorIndication:
    print errorIndication
else:
    if errorStatus:
        print '%s at %s\n' % (
            errorStatus.prettyPrint(),
            errorIndex and varBinds[int(errorIndex)-1] or '?'
            )
    else:
        for name, val in varBinds:
            print '%s = %s' % (name.prettyPrint(), val.prettyPrint())

我确保我的机器上运行着SNMP。我在控制台用以下命令检查:

snmpget -v2c -Cf -c public localhost 1.3.6.1.2.1.1.1.0

这个命令运行得很好。

但是如果我执行上面的Python代码,就会出现以下错误:

SmiError: MIB module "pysnmp/smi/mibs/SNMP-COMMUNITY-MIB.py" load error: MIB module "pysnmp/smi/mibs/SNMP-FRAMEWORK-MIB.py" load error: Bad OctetString initializer '[128, 0, 79, 184, 5, 192, 168, 1, 50, 371, 210, 26, 162, 157]'

每次执行时,最后的数字都会变化(看起来像是时间戳之类的东西)。我使用的是Python 2.7和最新版本的pySNMP(4.2.1)。有没有人知道这个示例代码有什么问题吗?

1 个回答

2

这是pysnmp 4.2.1中的一个错误,已经在最新的pysnmp候选版本中修复了(这个错误在Mac上出现的频率似乎更高,不知道为什么)。

这里有一个链接,指向目前最新的候选版本:

http://sourceforge.net/projects/pysnmp/files/pysnmp/4.2.2/pysnmp-4.2.2rc5.tar.gz/download

-ilya

撰写回答