【问题标题】:Async snmp table walk with PySnmp使用 PySnmp 进行异步 snmp 表遍历
【发布时间】:2020-07-22 01:22:37
【问题描述】:

我尝试使用 PySnmp 异步收集表,但示例只返回第一条记录。

谁能指出哪里出了问题?

import asyncio
from pysnmp.hlapi.asyncio import *

@asyncio.coroutine
def run():
    snmp_engine=SnmpEngine()
    count=0
    while True:
        errorIndication, errorStatus, errorIndex, varBinds = yield from nextCmd(
            snmp_engine,
            CommunityData('public'),
            UdpTransportTarget(('demo.snmplabs.com', 161)),
            ContextData(),
            ObjectType(ObjectIdentity('SNMPv2-MIB', 'system')),
        )

        for next_var in varBinds:
            print(next_var[0])

        count+=1
        #break
        if count > 10:
            break

asyncio.get_event_loop().run_until_complete(run())  

【问题讨论】:

    标签: python-3.5 python-asyncio pysnmp


    【解决方案1】:

    我最近遇到了同样的问题。我认为异步nextCmd 不像同步nextCmd 那样工作。您必须在每次迭代中更新 oid。此代码可能会有所帮助:

    import asyncio
    from pysnmp.hlapi.asyncio import *
    
    
    def inc_oid(oid):
        _oid = oid.split(".")
        lastint = int(_oid[-1])
        _oid[-1] = str(lastint + 1)
        return ".".join(_oid)
    
    snmp_engine=SnmpEngine()
    
    @asyncio.coroutine
    def snmp_walk_async(oid):
        endoid = inc_oid(oid)
        while True:
            errorIndication, errorStatus, errorIndex, varBinds = yield from nextCmd(
                snmp_engine,
                CommunityData("public"),
                UdpTransportTarget(("demo.snmplabs.com", 161)),
                ContextData(),
                ObjectType(ObjectIdentity(oid)),
                lexicographicMode=False,
            )
    
            #Some Error Checkings
    
            varBind = varBinds[0][0]
            oid, value = varBind
            oid = str(oid)
    
            if oid >= endoid:
                break
    
            for varBind in varBinds:
                print(' = '.join([x.prettyPrint() for x in varBind]))
    
    
    asyncio.get_event_loop().run_until_complete(snmp_walk_async("1.3.6.1.2.1.1"))  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-08
      • 2016-01-06
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多