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