I’m new to pysnmp(-lextudio) and have zero experience with asyncio as well. I’m trying to walk all OIDs from a base OID (ciscoSmartLicMIB – .1.3.6.1.4.1.9.9.831). Apparently the old way to do this was to use nextCmd
with lexicographicMode=False
, but that’s been changed in pysnmp-lextudio, and the new method is to use walkCmd. I’ve been unable to get even the simple example in the docs to work, however.
I tried the example in the docs:
from pysnmp.hlapi.asyncio import *
g = await walkCmd(SnmpEngine(),
CommunityData('public'),
UdpTransportTarget(('demo.pysnmp.com', 161)),
ContextData(),
ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr')))
next(g)
but get:
File "<stdin>", line 1
SyntaxError: 'await' outside function
Does anyone have a working example of walking all child OIDs of a top-level OID (and only child OIDs) using walkCmd
?
Gary Giesen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
I believe I’ve managed to refactor the example at Walking Operations based on Github issue etingof/pysnmp#233 to make it work:
import asyncio
from pysnmp.hlapi.asyncio import *
async def run(varBinds):
snmpEngine = SnmpEngine()
initialVarBinds = varBinds
while True:
errorIndication, errorStatus, errorIndex, varBindTable = await bulkCmd(
snmpEngine,
UsmUserData("usr-none-none"),
UdpTransportTarget(("demo.pysnmp.com", 161)),
ContextData(),
0,
50,
*varBinds,
)
if errorIndication:
print(errorIndication)
break
elif errorStatus:
print(
f"{errorStatus.prettyPrint()} at {varBinds[int(errorIndex) - 1][0] if errorIndex else '?'}"
)
else:
for varBindRow in varBindTable:
for idx, varBind in enumerate(varBindRow):
if initialVarBinds[0][idx].isPrefixOf(varBind[0]):
print(" = ".join([x.prettyPrint() for x in varBind]))
else:
return
varBinds = varBindTable[-1]
if isEndOfMib(varBinds):
break
return
asyncio.run(
run([ObjectType(ObjectIdentity("TCP-MIB")), ObjectType(ObjectIdentity("IP-MIB"))])
)
Gary Giesen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.