parsebgp.py 文件源码

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

项目:pyability 作者: syedur-rahman 项目源码 文件源码
def bgp_parse_logic(filename):
    """ bgp parse logic
    parse function to run in this example exercise """

    # initialize final reliability datastructure
    bgp_db = {}

    # initialize basic variables
    route_address = ''
    next_hop_address = ''

    with open(filename, 'r') as fn:
        # iterate through the file
        for line in fn:
            # store valid addresses
            valid_addresses = flexible_parse(line)
            print(valid_addresses)
            #valid_addresses = strict_parse(line)

            # check number of valid addresses is 2
            if len(valid_addresses) == 2:
                # initialize the valid addresses
                route_address = valid_addresses[0]
                next_hop_address = valid_addresses[1]

                # initialize route address in dictionary if not already done
                if route_address not in bgp_db:
                    bgp_db[route_address] = 0

                # increment redundancy count
                bgp_db[route_address] += 1

            # check number of valid addresses is 1
            # this should only be invoked with a different format of
            # 'show ip bgp' - aka CISCO DEVICES
            # this will also break strict parsing FYI
            elif len(valid_addresses) == 1:
                # initialize valid addresses
                next_hop_address = valid_addresses[0]

                # initialize route address in dictionary if not already done
                if route_address not in bgp_db:
                    bgp_db[route_address] = 0

                # increment redundancy count
                bgp_db[route_address] += 1


    # save our data as a yaml file to be reused for graping purposes!
    with open('fullbgpredundancy.yml', 'w') as fn:
        yaml=YAML()
        yaml.default_flow_style = False
        yaml.dump(bgp_db, fn)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号