简体   繁体   中英

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. I'm trying to pass a list of network devices to bulkCmd to obtain information on all the physical interfaces.

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.

I have successfully polled all interfaces when sending a single bulkCmd to single network device.

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. You probably got the idea from the PySNMP documentation , which doesn't do a great job of showing how to retrieve all the results.

You should use a for loop to loop over all of the iterations, as follows:

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.

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). Just ditch the next() call and run for loop over 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).

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 . You'd use the same getBulk() call, it's just the underlaying network I/O that does the parallelism.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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