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/rpf-check.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
99 lines (87 sloc)
3.48 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 few 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-rpf_check.csv', 'w')) | |
writer.writerow(["Hostname", "Interface", "inet rpf-check", "inet6 rpf-check", "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('family inet', {}) | |
if_config_inet = if_config['family inet'] | |
if_config_inet.setdefault('rpf-check', '') | |
if_config.setdefault('family inet6', {}) | |
if_config_inet6 = if_config['family inet6'] | |
if_config_inet6.setdefault('rpf-check', '') | |
writer.writerow([ | |
rtr_hostname, | |
if_name, | |
if_config_inet['rpf-check'], | |
if_config_inet6['rpf-check'], | |
re.sub(r'^"|";$', r'', if_config['description'], 2) | |
]) | |
# print(if_name, if_config['description'], if_config['rpf-check']) | |
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('family inet', {}) | |
unit_config_inet = unit_config['family inet'] | |
unit_config_inet.setdefault('rpf-check', '') | |
unit_config.setdefault('family inet6', {}) | |
unit_config_inet6 = unit_config['family inet6'] | |
unit_config_inet6.setdefault('rpf-check', '') | |
writer.writerow([ | |
rtr_hostname, | |
"{}.{}".format(if_name, unit_num), | |
unit_config_inet['rpf-check'], | |
unit_config_inet6['rpf-check'], | |
re.sub(r'^"|";$', r'', unit_config['description'], 2) | |
]) | |
# print("", if_unit, unit_config['description'], unit_config['rpf-check']) | |
# print() |