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)
评论列表
文章目录