PYSNMP如何使用SNMPWALK获取网络设备的所有OID



我想知道如何在Pysnmp中获取某个网络设备的所有OID。
这是我的代码:

        errorIndication, errorStatus, errorIndex, varBind = cmdGen.nextCmd(
                    cmdgen.CommunityData('public'),
                    cmdgen.UdpTransportTarget((ip, 161)),
                    cmdgen.MibVariable('IF-MIB', '').loadMibs(), #get all oids
                    lexicographicMode=True, maxRows=10000,
                    ignoreNonIncreasingOid=True
                )
        for varBindTableRow in varBind:
            for name, val in varBindTableRow:
                print name, val
        print(len(varBind)) #prints number of oids

问题是我从来没有得到设备的所有OID(实际上都没有关闭)。
使用此代码,我通常会得到90左右,但是当我使用snmpwalk.exe(从互联网下载)时,我通常每次获得700个。
我尝试以许多方式重写我的代码,但没有任何作用。
有人可以告诉我如何使用PYSNMP获取网络设备的所有OID?

您的代码应该从IF-MIB的第一个OID开始,最多可使您达到10000个OID。尝试从第一个OID开始行走您的代理商,并确保步行途中没有间歇性错误。

另外,请记住,您的PYSNMP代码使用SNMP V2C。我不确定您在snmpwalk.exe中使用了什么SNMP版本。虽然不太可能,但是从理论上讲,您的SNMP代理对SNMP V1和V2C查询的反应不同。我认为SNMP社区在两种情况下都是相同的。

如果没有任何帮助,请尝试启用Pysnmp调试以查看要求的OID,返回了哪些OID以及是否以及为什么Pysnmp过早停止:

    from pysnmp import debug
    debug.setLogger(debug.Debug('msgproc', 'dsp', 'io', 'app'))
    errorIndication, errorStatus, errorIndex, varBind = cmdGen.nextCmd(
                cmdgen.CommunityData('public'),
                cmdgen.UdpTransportTarget((ip, 161)),
                '1.3.6',
                lexicographicMode=True,
                ignoreNonIncreasingOid=True
            )
    # make sure there's no timeout or other errors occurring at some point
    if errorIndication:
        print('WARNING: %s' % errorIndication)
    for varBindTableRow in varBind:
        for name, val in varBindTableRow:
            print(name, val)
    print(len(varBind)) #prints number of oids

最新更新