forked from jwegner89/netbox-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull_juniper_router_vlans.py
executable file
·193 lines (170 loc) · 5.42 KB
/
pull_juniper_router_vlans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/python3.6
"""
Parse router configs for vlans
"""
import argparse
import csv
import getpass
import ipaddress
import json
import netmiko
import os
import re
import socket
import sys
def pull_config(device, user, password):
try:
# retrieve IP from hostname
ip = socket.gethostbyname(device)
conn_info = {
'device_type': 'juniper_junos',
'ip': ip,
'username': user,
'password': password,
}
net_connect = netmiko.ConnectHandler(**conn_info)
# at some point, vlans became irbs, so try both and concatenate them
irbs = net_connect.send_command('show configuration interfaces irb')
vlans = net_connect.send_command('show configuration interfaces vlan')
return irbs + vlans
except socket.gaierror as e:
print('Device {device} is not resolvable.'.format(device=device))
sys.exit(str(e))
def parse_vlans(device, config):
"""
Example Juniper config section:
unit 91 {
description rtr-example-servers;
family inet {
... snip ...
address 203.0.113.1/27;
}
family inet6 {
... snip ...
address 2001:db8:beef:feed::1/64;
}
}
"""
# setup regex
re_site = re.compile(r'(mfc|rtr)-(?P<site>\w+)-\w+')
re_vid = re.compile(r'^\s*unit (?P<vid>\d+) {$')
re_desc = re.compile(r'^\s*description (?P<desc>[\w-]+);$')
re_address = re.compile(r'^\s*address (?P<address>[0-9A-Fa-f:/.]+);$')
re_blank = re.compile(r'^\s*$')
# parse site
site = ''
group_name = device.split('.')[0]
site_match = re_site.match(group_name)
if site_match:
site = site_match.group('site')
if len(site) == 0:
sys.exit('Unable to parse site for {group_name}'.format(group_name=group_name))
# create empty dict for scoping issues
vlan = dict()
vlans = list()
for line in config.split('\n'):
# skip blank lines
if re_blank.match(line):
continue
# check if opening new unit block
if re_vid.match(line):
# in a new unit block, pull out vlan id
vid = re_vid.match(line).group('vid')
name = '{group_name}-v{vid}'.format(
group_name=group_name,
vid=vid,
)
# store known information in new dict
vlan = {
'site': site.upper(),
'group_name': group_name,
'vid': vid,
'name': name,
'tenant': '',
'status': 'Active',
'role': '',
}
# add new dict to list
vlans.append(vlan)
# check if setting description
elif re_desc.match(line):
vlan['description'] = re_desc.match(line).group('desc')
# check if setting address
elif re_address.match(line):
address = re_address.match(line).group('address')
try:
ip_info = ipaddress.ip_interface(address)
if isinstance(ip_info, ipaddress.IPv4Interface):
vlan['ipv4_network'] = str(ip_info.network)
vlan['ipv4_gateway'] = ip_info.with_prefixlen
if isinstance(ip_info, ipaddress.IPv6Interface):
vlan['ipv6_network'] = str(ip_info.network)
vlan['ipv6_gateway'] = ip_info.with_prefixlen
except ValueError as e:
print('Exception converting {address} to IP'.format(address=address))
sys.exit(str(e))
return vlans
def write_vlans(vlans, output_file):
with open(output_file, 'w', newline='') as csvfile:
fieldnames = [
'site',
'group_name',
'vid',
'name',
'tenant',
'status',
'role',
'description',
'ipv4_network',
'ipv4_gateway',
'ipv6_network',
'ipv6_gateway',
]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames, dialect='unix')
writer.writeheader()
writer.writerows(vlans)
def main():
parser = argparse.ArgumentParser(
description='Parse router configs for vlans',
)
parser.add_argument(
'-u',
'--user',
type=str,
default='admin',
help='user for device login',
)
parser.add_argument(
'-i',
'--input_file',
type=str,
default='routers.txt',
help='file containing devices to poll',
)
parser.add_argument(
'-o',
'--output_file',
type=str,
default='routers_vlans.csv',
help='location for output file ',
)
args = parser.parse_args()
devices = list()
with open(args.input_file, 'r') as infile:
devices = infile.readlines()
if len(devices) == 0:
sys.exit(
'Unable to read {input_file} or {input_file} is empty'.format(
input_file=args.input_file,
)
)
password = getpass.getpass('Password for {user}: '.format(user=args.user))
vlans = list()
for device in devices:
device = device.lower().strip()
config = pull_config(device, args.user, password)
vlans.extend(parse_vlans(device, config))
write_vlans(vlans, args.output_file)
if __name__ == '__main__':
main()
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4