Permalink
Cannot retrieve contributors at this time
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?
james-scripts/interface-apply-groups.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
87 lines (75 sloc)
2.85 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |