2

I am using pysnmp to query multiple oid ranges. I need to use nxtCmd because these I need to walk through, for example, all the IF addresses. However, when I use lexicographicMode=False to prevent crossing OID barriers as I walk, I cannot reuse the same iterator to query a new range using 'send' because I get a StopIteration error (the iterator is exhausted). Is the solution to instantiate a new iterator every time? Am I approaching this incorrectly?

Code:

iterator = nextCmd(SnmpEngine(),
                          CommunityData('public'),
                          UdpTransportTarget(('localhost', 161)),
                          ContextData(),
                          ObjectType(ObjectIdentity('1.3.6.1.2.1.4.20.1.1')),
                          lexicographicMode=False)

for (errorIndication,
     errorStatus,
     errorIndex,
     varBinds) in iterator:

    if errorIndication:
        print(errorIndication)
        break
    
    elif errorStatus:
        print('%s at %s' % (errorStatus.prettyPrint(),
                            errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
        break
    
    else:
        for varBind in varBinds:
            print(type(varBind))
            print(' = '.join([x.prettyPrint() for x in varBind]))

errorIndication, errorStatus, errorIndex, varBinds = iterator.send(ObjectType(ObjectIdentity('1.3.6.1.2.1.4.20.1.1')))
print(varBinds[1].prettyPrint)

First loop works, the send command produces a StopIteration exception

Mason3k
  • 151
  • 1
  • 8

1 Answers1

0

Yes, if the iterator is exhausted, you cannot reuse it.

However, even though iterators and generators are similar, only generators support the send method.

I would first check that nextCmd definetely returns a generator and if it does, check this article out: How to Use .send(), it'll give you an example.

Pablo
  • 983
  • 10
  • 24