This repository has been archived by the owner on Nov 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
export_ipplan.py
executable file
·258 lines (229 loc) · 7.08 KB
/
export_ipplan.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python3
import argparse
import csv
import ipaddress
import math
import operator
import re
import sys
def parse_prefixes(sql):
values = list()
for line in sql:
if 'INSERT INTO `base`' == line[:18]:
# line is of form
# INSERT INTO `base` VALUES (...),...,(...);\n
line = line[27:-3]
values.extend(line.split('),('))
if not values:
sys.exit('Error: cannot convert SQL to CSV')
# use csv module to convert SQL inserts
fieldnames = [
'baseaddr',
'subnetsize',
'descrip',
'baseindex',
'admingrp',
'customer',
'lastmod',
'userid',
'swipmod',
'baseopt',
]
reader = csv.DictReader(
values,
fieldnames=fieldnames,
dialect='unix',
delimiter=',',
quotechar="'",
)
# regex to match vlans if present
re_vlan = re.compile(r'(?P<group>(mfc|rtr)-\w+-\w+)-v(?P<vlan>\d+) : (?P<desc>.*)')
re_info = re.compile(r'(mfc|rtr)-(?P<site>\w+)-(?P<loc>\w+)')
vlans = list()
prefixes = list()
for row in reader:
# subnetsize given in available IPs for subnet, e.g. 256, 512
# so we need to convert it to its corresponding CIDR mask
mask = 32 - int(math.log(int(row['subnetsize']), 2))
addr_from_int = str(ipaddress.ip_address(int(row['baseaddr'])))
network = '{network}/{mask}'.format(network=addr_from_int, mask=mask)
descrip = row['descrip'].strip()
prefix = {
'prefix': network,
'vrf': '',
'tenant': '',
'site': '',
'vlan_group': '',
'vlan_vid': '',
'status': 'Active',
'role': '',
'is_pool': 'false',
'description': descrip,
}
prefixes.append(prefix)
vlan_match = re_vlan.match(descrip)
info_match = re_info.match(descrip)
if vlan_match:
vlan_group = vlan_match.group('group')
vlan_vid = vlan_match.group('vlan')
desc = vlan_match.group('desc')
info_match = re_info.match(vlan_group)
site = info_match.group('site')
loc = info_match.group('loc')
vlan_desc = desc.lower().replace(' ', '-')
prefix['site'] = site.upper()
prefix['vlan_group'] = vlan_group
prefix['vlan_vid'] = vlan_vid
prefix['description'] = desc
vlan = {
'site': site.upper(),
'group_name': vlan_group,
'vid': vlan_vid,
'name': desc,
'tenant': '',
'status': 'Active',
'role': '',
'description': '',
}
vlans.append(vlan)
elif info_match:
info_match = re_info.match(vlan_group)
site = info_match.group('site')
loc = info_match.group('loc')
vlan_desc = descrip.lower().replace(' ', '-')
prefix['site'] = site.upper()
prefix['vlan_group'] = vlan_group
prefix['vlan_vid'] = vlan_vid
prefix['description'] = descrip
vlan = {
'site': site.upper(),
'group_name': vlan_group,
'vid': vlan_vid,
'name': descrip,
'tenant': '',
'status': 'Active',
'role': '',
'description': '',
}
vlans.append(vlan)
# sort lists according to vlan ID for easy comparison
prefixes = sorted(prefixes, key=operator.itemgetter('vlan_vid'))
vlans = sorted(vlans, key=operator.itemgetter('vid'))
with open('ipplan_prefixes.csv', 'w', newline='') as csvfile:
fieldnames = [
'prefix',
'vrf',
'tenant',
'site',
'vlan_group',
'vlan_vid',
'status',
'role',
'is_pool',
'description',
]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames, dialect='unix')
writer.writeheader()
writer.writerows(prefixes)
with open('ipplan_vlans.csv', 'w', newline='') as csvfile:
fieldnames = [
'site',
'group_name',
'vid',
'name',
'tenant',
'status',
'role',
'description',
]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames, dialect='unix')
writer.writeheader()
writer.writerows(vlans)
def parse_addresses(sql):
values = list()
for line in sql:
if 'INSERT INTO `ipaddr`' == line[:20]:
# line is of form
# INSERT INTO `ipaddr` VALUES (...),...,(...);\n
line = line[29:-3]
values.extend(line.split('),('))
if not values:
sys.exit('Error: cannot convert SQL to CSV')
# use csv module to convert from SQL
fieldnames = [
'ipaddr',
'userinf',
'location',
'telno',
'descrip',
'baseindex',
'lastmod',
'userid',
'hname',
'macaddr',
'lastpol',
]
reader = csv.DictReader(
values,
fieldnames=fieldnames,
dialect='unix',
delimiter=',',
quotechar="'",
)
# create list to store IP dictionaries
ips = list()
for row in reader:
desc = row['descrip'].strip()
name = row['hname'].strip()
description = ''
if name and not desc:
description = name
elif desc and not name:
if desc != 'Unknown - added by IPplan command line poller':
description = desc
elif desc and name:
if desc == name or desc == 'Unknown - added by IPplan command line poller':
description = name
else:
description = '{} - {}'.format(name, desc)
address = str(ipaddress.ip_address(int(row['ipaddr']))) + '/32'
ip = {
'address': address,
'status': 'Active',
'description': description,
}
ips.append(ip)
with open('ipplan_addresses.csv', 'w', newline='') as csvfile:
fieldnames = [
'address',
'status',
'description',
]
ips = sorted(ips, key=operator.itemgetter('address'))
writer = csv.DictWriter(
csvfile,
fieldnames=fieldnames,
dialect='unix',
)
writer.writeheader()
writer.writerows(ips)
def main():
parser = argparse.ArgumentParser(
description='generate CSV for Netbox import for IPPlan MySQL dump',
)
parser.add_argument(
'input',
type=str,
help='MySQL dump file for import',
)
args = parser.parse_args()
sql = ''
with open(args.input, 'r') as infile:
sql = infile.readlines()
if not sql:
sys.exit('Error reading input file')
parse_prefixes(sql)
parse_addresses(sql)
if __name__ == '__main__':
main()
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4