snmp.py 文件源码

python
阅读 29 收藏 0 点赞 0 评论 0

项目:prngmgr 作者: wolcomm 项目源码 文件源码
def get_bgp_table(host):
    """Return the contents of the cbgpPeer2Table table."""
    obj = ObjectIdentity('CISCO-BGP4-MIB', 'cbgpPeer2Table')
    target = UdpTransportTarget((host, SNMP['port']))

    results = defaultdict(dict)

    for (errorIndication, errorStatus,
         errorIndex, varBinds) in bulkCmd(snmp, usm, target, context, 0, 25,
                                          ObjectType(obj),
                                          lexicographicMode=False,
                                          lookupMib=True):
        if errorIndication:
            raise RuntimeError(errorIndication)
        elif errorStatus:
            raise RuntimeError('%s at %s'
                               % (errorStatus.prettyPrint(),
                                  errorIndex
                                  and varBinds[-1][int(errorIndex)-1] or '?'))
        else:
            for (key, val) in varBinds:
                if not isinstance(val, EndOfMibView):
                    (mib, name, index) = \
                        key.loadMibs('BGP4-MIB').getMibSymbol()
                    a = index[1].prettyPrint()
                    results[a]['cbgpPeer2Type'] = index[0]
                    results[a][name] = val
    for address in results:
        if results[address]['cbgpPeer2Type'] == 1:
            results[address]['cbgpPeer2RemoteAddr'] = \
                ipaddress.IPv4Address(int(address, 16))
        elif results[address]['cbgpPeer2Type'] == 2:
            results[address]['cbgpPeer2RemoteAddr'] = \
                ipaddress.IPv6Address(int(address, 16))
    return results
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号