当前位置: 首页 > 工具软件 > pySNMP > 使用案例 >

pysnmp实现walk

孔鹤龄
2023-12-01

pysnmp实现walk

from pysnmp.hlapi import *
def walk(ip, oid):
    res=[]
    for (errorIndication,errorStatus,errorIndex,varBinds) in nextCmd(SnmpEngine(), 
        CommunityData('123'), UdpTransportTarget((ip, 161)), ContextData(),
        ObjectType(ObjectIdentity(oid)).addMibSource('C:/Users/22589/Desktop/urls/dist/MIBs', 'pysnmp_mibs'),lexicographicMode=False):
        if errorIndication:
            print(errorIndication, file=sys.stderr)
            break
        elif errorStatus:
            print('%s at %s' % (errorStatus.prettyPrint(),
                                errorIndex and varBinds[int(errorIndex) - 1][0] or '?'), 
                                file=sys.stderr)             
            break
        else:
            for varBind in varBinds:
                #print(varBind)
                res.append(str(varBind))
    return res
 类似资料: