bgp_route.py 文件源码

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

项目:sonic-mgmt 作者: Azure 项目源码 文件源码
def main():
    module = AnsibleModule(
            argument_spec=dict(
                neighbor=dict(required=False, default=None),
                direction=dict(required=False, choices=['adv', 'rec']),
                prefix=dict(required=False, default=None)
                ),
            supports_check_mode=False
            )
    m_args = module.params
    neighbor = m_args['neighbor']
    direction = m_args['direction']
    prefix = m_args['prefix']
    regex_ip = re.compile('[0-9a-fA-F.:]+')
    regex_iprange = re.compile('[0-9a-fA-F.:]+\/\d+')
    regex_ipv4 = re.compile('[12][0-9]{0,2}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/?\d+?')
    if neighbor == None and direction == None and prefix == None:
        module.fail_json(msg="No support of parsing 'show ip bgp' full prefix table yet")
        return
    if neighbor and ((not netaddr.valid_ipv4(neighbor)) and (not netaddr.valid_ipv6(neighbor))):
        err_message = "Invalid neighbor address %s ??" % neighbor
        module.fail_json(msg=err_message)
        return
    if (neighbor and not direction) or (neighbor and 'adv' not in direction.lower()):
        err_message = 'No support of parsing this command " show ip(v6) bgp neighbor %s %s" yet' % (neighbor, direction)
        module.fail_json(msg=err_message)
        return
    try:
        bgproute = BgpRoutes(neighbor, direction, prefix)

        if prefix:
            if regex_ipv4.match(prefix):
                command = "docker exec -i bgp vtysh -c 'show ip bgp " + str(prefix) + "'"
            else:
                command = "docker exec -i bgp vtysh -c 'show ipv6 bgp " + str(prefix) +  "'"
            rc, out, err = module.run_command(command)
            if rc != 0:
                err_message = "command %s failed rc=%d, out=%s, err=%s" %(command, rt, out, err)
                module.fail_json(msg=err_message)
                return
            bgproute.parse_bgp_route_prefix(out)

        elif neighbor:
            if netaddr.valid_ipv4(neighbor):
                command = "docker exec -i bgp vtysh -c 'show ip bgp neighbor " + str(neighbor) + " " + str(direction) + "'"
            else:
                command = "docker exec -i bgp vtysh -c 'show ipv6 bgp neighbor " + str(neighbor) + " " + str(direction) + "'"
            rc, out, err = module.run_command(command)
            if rc !=  0:
                err_message = "command %s failed rc=%d, out=%s, err=%s" %(command, rt, out, err)
                module.fail_json(msg=err_message)
                return
            bgproute.parse_bgp_route_adv(out)

        results = bgproute.get_facts()
        module.exit_json(ansible_facts=results)
    except Exception as e:
        fail_msg = "cannot correctly parse BGP Routing facts!\n"
        fail_msg += str(e)
        module.fail_json(msg=fail_msg)
    return
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号