简体   繁体   English

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

[英]PySNMP 4.4 with Python 2.7 bulkCmd output not including child OID's

To start off with, I'm new to Python and PySNMP. 首先,我是Python和PySNMP的新手。 I'm trying to pass a list of network devices to bulkCmd to obtain information on all the physical interfaces. 我正在尝试将网络设备列表传递给bulkCmd以获取有关所有物理接口的信息。

Currently it is only collecting the first interface and then moves on to the next network device in the list. 当前,它仅收集第一个接口,然后移至列表中的下一个网络设备。 I have made changes with lexicographic and maxCalls, repetitions but none make any difference. 我对词典和maxCalls进行了更改,但重复没有变化。

I have successfully polled all interfaces when sending a single bulkCmd to single network device. 将单个bulkCmd发送到单个网络设备时,我已成功轮询所有接口。

Code: 码:

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)

Output: 输出:

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 returns an iterator and you were using next() on it which retrieves only the first of the iterations. bulkSNMP返回一个迭代器,并且您正在其上使用next() ,它仅检索第一个迭代。 You probably got the idea from the PySNMP documentation , which doesn't do a great job of showing how to retrieve all the results. 您可能从PySNMP文档中得到了这个主意,该文档在显示如何检索所有结果方面做得并不好。

You should use a for loop to loop over all of the iterations, as follows: 您应该使用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)

Also, be careful about indentation when posting Python-related questions, as it is significant. 另外,在发布与Python有关的问题时,请注意缩进,因为缩进非常重要。

You need to iterate over the generator produced by the bulkCmd function to repeat SNMP queries to pull SNMP managed objects that did not fit into previous response packet(s). 您需要遍历bulkCmd函数产生的生成器,以重复SNMP查询以提取不适合先前响应包的SNMP受管对象。 Just ditch the next() call and run for loop over bulkCmd() . 只要沟next()调用和运行for遍历bulkCmd()

Side note 1: you may not need lexicographicMode=True if you want to fetch managed objects that reside just under the MIB table columns (eg IF-MIB::ifDescr etc). 旁注1:如果要获取位于MIB表列正下方的托管对象(例如IF-MIB::ifDescr等),则可能不需要lexicographicMode=True

Side note 2: if you have many SNMP agents on your network, you may consider speeding up the process of data retrieval by talking to the in parallel . 旁注2:如果您的网络上有许多SNMP代理,则可以考虑通过与并行交谈来加快数据检索的过程。 You'd use the same getBulk() call, it's just the underlaying network I/O that does the parallelism. 您将使用相同的getBulk()调用,只是底层网络I / O进行了并行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM