python netsnmp与pysnmp

据说,如果论性能,netsnmp更强,而且netsnmp天生支持walk, 但是在安装方式上, pysnmp简单直接,netsnmp需要先安装netsnmp,然后安装相应的python模块,比较麻烦。

参考 http://www.huilog.com/?p=629, 基本上分为下载, python setup.py build, python setup.py install 两步

而且pysnmp也可以自己写代码实现walk 

def walk(host, oid, community):
    for (errorIndication,errorStatus,errorIndex,varBinds) in nextCmd(SnmpEngine(), 
        CommunityData(community),
        UdpTransportTarget((host, 161)),
        ContextData(),
        ObjectType(ObjectIdentity(oid)),
        lexicographicMode=False  # 限制其只在指定范围内walk
        ):
        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:
            # return varBinds

            for varBind in varBinds:
                yield varBind
            #     print(varBind)
def get(host, oid, snmpCommunity):
    errorIndication, errorStatus, errorIndex, varBinds = next(
        getCmd(SnmpEngine(),
               CommunityData(snmpCommunity),
               UdpTransportTarget((host, 161)),
               ContextData(),
               ObjectType(ObjectIdentity(oid))
               )
    )

    if errorIndication:
        print(errorIndication)
    elif errorStatus:
        print('%s at %s' % (errorStatus.prettyPrint(),
                            errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
    else:
        for varBind in varBinds:
            yield varBind
原文地址:https://www.cnblogs.com/yeyong/p/10938775.html