带有Python 2.7 bulkCmd输出的PySNMP 4.4不包括子OID。



首先,我是Python和PySNMP的新手。我正在尝试将网络设备列表传递给bulkCmd以获取有关所有物理接口的信息。

目前,它只收集第一个接口,然后移动到列表中的下一个网络设备。我已经对词典编纂和maxCalls进行了更改,重复,但没有一个有任何区别。

在将单个 bulkCmd 发送到单个网络设备时,我已经成功轮询了所有接口。

法典:

from pysnmp.hlapi import *
routers = ["router1", "router2"]
#adds routers to getCmd and bulkCmd
def snmpquery (hostip):
    errorIndication, errorStatus, errorIndex, varBinds = next (
        bulkCmd(SnmpEngine(),
            CommunityData('communitystring'),
            UdpTransportTarget((hostip, 161)),
            ContextData(),
            0, 50,  
            ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')),
            ObjectType(ObjectIdentity('IF-MIB', 'ifAlias')),
            ObjectType(ObjectIdentity('IF-MIB', 'ifOperStatus')),
            lexicographicMode=True
        )
    )
    # Check for errors and print out results
    if errorIndication:
        print(errorIndication)
    elif errorStatus:
        print('%s at %s' % (errorStatus.prettyPrint(),
                            errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
    else:
        for varBind in varBinds:
            print(' = '.join([x.prettyPrint() for x in varBind]))

# calls snmpquery for all routers in list
for router in routers:
    snmpquery(router)

输出:

IF-MIB::ifDescr.1 = GigabitEthernet0/0
IF-MIB::ifAlias.1 = InterfaceDesc
IF-MIB::ifOperStatus.1 = 'up'
IF-MIB::ifDescr.1 = GigabitEthernet0/0
IF-MIB::ifAlias.1 = InterfaceDesc
IF-MIB::ifOperStatus.1 = 'up'

bulkSNMP返回一个迭代器,并且您在其上使用next(),该迭代器仅检索第一个迭代。您可能从 PySNMP 文档中得到了这个想法,该文档在展示如何检索所有结果方面做得不好。

应使用 for 循环遍历所有迭代,如下所示:

from pysnmp.hlapi import *
routers = ["router1", "router2"]
def snmpquery (hostip):
    snmp_iter = bulkCmd(SnmpEngine(),
                        CommunityData('communitystring'),
                        UdpTransportTarget((hostip, 161)),
                        ContextData(),
                        0, 50,  
                        ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')),
                        ObjectType(ObjectIdentity('IF-MIB', 'ifAlias')),
                        ObjectType(ObjectIdentity('IF-MIB', 'ifOperStatus')),
                        lexicographicMode=True)
    for errorIndication, errorStatus, errorIndex, varBinds in snmp_iter:
        # Check for errors and print out results
        if errorIndication:
            print(errorIndication)
        elif errorStatus:
            print('%s at %s' % (errorStatus.prettyPrint(),
                            errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
        else:
            for varBind in varBinds:
                print(' = '.join([x.prettyPrint() for x in varBind]))
# calls snmpquery for all routers in list
for router in routers:
    snmpquery(router)

另外,在发布与 Python 相关的问题时要小心缩进,因为它很重要。

您需要

遍历bulkCmd函数生成的生成器,以重复 SNMP 查询以拉取不适合先前响应数据包的 SNMP 托管对象。只需放弃next()呼叫并运行for循环bulkCmd().

旁注 1 lexicographicMode=True:如果要获取驻留在 MIB 表列下的托管对象(例如 IF-MIB::ifDescr等(。

旁注 2:如果您的网络上有许多 SNMP 代理,您可以考虑通过与 并行通信来加快数据检索过程。您将使用相同的getBulk()调用,它只是执行并行性的底层网络 I/O。

最新更新