Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#!/usr/bin/env python3
import re
import os
import logging
import jq
import csv
DIR = "../rtsw/configs/"
def parse_junos_config(rtr_config_file):
log = logging.getLogger('junos_config_parser')
lines = open(rtr_config_file, "r").readlines()
root = {}
current = root
nest_stack = []
for l in lines:
l = l.strip()
m_open_bracket = re.match(r'^(.*) {$', l)
m_close_bracket = re.match(r'^}$', l)
if m_open_bracket:
n = m_open_bracket.group(1)
nest_stack.append(current)
new_current = current[n] = {}
current = new_current
elif re.match(r'^#\s+key <removed>;', l):
# HAX, rancid took out an open bracket
n = "key <removed>"
nest_stack.append(current)
new_current = current[n] = {}
current = new_current
elif m_close_bracket:
if len(nest_stack) == 0:
log.error("Uhhh, too many close curley braces. Just gonna return what we got...")
break
current = nest_stack.pop()
elif l.startswith("#"):
# Ignore comments
pass
else:
arr = l.split(' ', 1)
k = arr[0]
v = arr[1] if len(arr) > 1 else None
current[k] = v
assert len(nest_stack) == 0
return root
logging.basicConfig()
logging.getLogger('junos_config_parser').setLevel(logging.INFO)
writer = csv.writer(open('interface-apply-groups.csv', 'w'))
writer.writerow(["Hostname", "Interface", "Apply Groups", "Description"])
for rtr_config_file in os.listdir(DIR):
rtr_hostname = re.match(r'^(.*)\.net\.internet2\.edu\.config$', rtr_config_file).group(1)
config = parse_junos_config(os.path.join(DIR, rtr_config_file))
print("Processing {}".format(rtr_hostname))
for if_name, if_config in config['interfaces'].items():
if_config.setdefault('description', '')
if_config.setdefault('apply-groups', '')
writer.writerow([
rtr_hostname,
if_name,
if_config['apply-groups'],
re.sub(r'^"|";$', r'', if_config['description'], 2)
])
# print(if_name, if_config['description'], if_config['apply-groups'])
for if_unit, unit_config in if_config.items():
m_unit = re.match(r'unit (\d+)', if_unit)
if not m_unit: continue
unit_num = int(m_unit.group(1))
unit_config.setdefault('description', '')
unit_config.setdefault('apply-groups', '')
writer.writerow([
rtr_hostname,
"{}.{}".format(if_name, unit_num),
unit_config['apply-groups'],
re.sub(r'^"|";$', r'', unit_config['description'], 2)
])
# print("", if_unit, unit_config['description'], unit_config['apply-groups'])
# print()