-
Notifications
You must be signed in to change notification settings - Fork 2
/
phpipam2device42.py
428 lines (351 loc) · 13.3 KB
/
phpipam2device42.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__version__ = 1.0
"""
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
#############################################################################################################
# v1.0 of python script that connects to PHPIPAM DB and migrates data to Device42 appliance using APIs
# Refer to README for further instructions
#############################################################################################################
import os
import sys
import imp
import json
import codecs
import base64
import netaddr
import requests
import pymysql as sql
conf = imp.load_source('conf', 'conf')
dev_types = ['physical', 'virtual', 'blade', 'cluster', 'other']
ip_types = ['static', 'dhcp', 'reserved']
try:
requests.packages.urllib3.disable_warnings()
except:
pass
class Logger:
def __init__(self, logfile, stdout):
self.logfile = logfile
self.stdout = stdout
self.check_log_file()
def check_log_file(self):
while 1:
if os.path.exists(self.logfile):
reply = raw_input("[!] Log file already exists. Overwrite or append [O|A]? ")
if reply.lower().strip() == 'o':
with open(self.logfile, 'w'):
pass
break
elif reply.lower().strip() == 'a':
break
else:
break
if conf.DEBUG and os.path.exists(conf.DEBUG_LOG):
with open(conf.DEBUG_LOG, 'w'):
pass
def writer(self, msg):
if conf.LOGFILE and conf.LOGFILE != '':
with codecs.open(self.logfile, 'a', encoding='utf-8') as f:
msg = msg.decode('UTF-8', 'ignore')
f.write(msg + '\r\n') # \r\n for notepad
if self.stdout:
try:
print msg
except:
print msg.encode('ascii', 'ignore') + ' # < non-ASCII chars detected! >'
@staticmethod
def debugger(msg):
if conf.DEBUG_LOG and conf.DEBUG_LOG != '':
with codecs.open(conf.DEBUG_LOG, 'a', encoding='utf-8') as f:
title, message = msg
row = '\n-----------------------------------------------------\n%s\n%s' % (title, message)
f.write(row + '\r\n\r\n') # \r\n for notepad
class REST:
def __init__(self):
self.password = conf.D42_PWD
self.username = conf.D42_USER
self.base_url = conf.D42_URL
def uploader(self, data, url):
payload = data
headers = {
'Authorization': 'Basic ' + base64.b64encode(self.username + ':' + self.password),
'Content-Type': 'application/x-www-form-urlencoded'
}
r = requests.post(url, data=payload, headers=headers, verify=False)
msg = unicode(payload)
logger.writer(msg)
msg = 'Status code: %s' % str(r.status_code)
logger.writer(msg)
msg = str(r.text)
logger.writer(msg)
try:
return r.json()
except Exception as e:
print '\n[*] Exception: %s' % str(e)
pass
def fetcher(self, url):
headers = {
'Authorization': 'Basic ' + base64.b64encode(self.username + ':' + self.password),
'Content-Type': 'application/x-www-form-urlencoded'
}
r = requests.get(url, headers=headers, verify=False)
return r.text
def post_vrf(self, data):
url = self.base_url + '/api/1.0/vrf_group/'
msg = '\r\nPosting data to %s ' % url
logger.writer(msg)
return self.uploader(data, url)
def get_vrfs(self):
url = self.base_url + '/api/1.0/vrf_group/'
msg = '\r\nGetting data from %s ' % url
logger.writer(msg)
return self.fetcher(url)
def post_subnet(self, data):
url = self.base_url + '/api/1.0/subnets/'
msg = '\r\nPosting data to %s ' % url
logger.writer(msg)
self.uploader(data, url)
def get_subnets(self):
url = self.base_url + '/api/1.0/subnets/'
msg = '\r\nGetting data from %s ' % url
logger.writer(msg)
return self.fetcher(url)
def get_vlans(self):
url = self.base_url + '/api/1.0/vlans/'
msg = '\r\nGetting data from %s ' % url
logger.writer(msg)
return self.fetcher(url)
def post_vlan(self, data):
url = self.base_url + '/api/1.0/vlans/'
msg = '\r\nPosting VLAN data to %s ' % url
logger.writer(msg)
self.uploader(data, url)
def post_ip(self, data):
url = self.base_url + '/api/1.0/ips/'
msg = '\r\nPosting IP data to %s ' % url
logger.writer(msg)
self.uploader(data, url)
def post_device(self, data):
url = self.base_url + '/api/1.0/devices/'
msg = '\r\nPosting IP data to %s ' % url
logger.writer(msg)
self.uploader(data, url)
class DB:
"""
Fetching data from phpipam and converting them to Device42 API format.
"""
def __init__(self):
self.con = None
self.default_vrf_group = conf.DEFAULT_VRF_GROUP
def connect(self):
"""
Connection to phpipam database
:return:
"""
self.con = sql.connect(host=conf.DB_IP, port=int(conf.DB_PORT), db=conf.DB_NAME,
user=conf.DB_USER, passwd=conf.DB_PWD)
@staticmethod
def convert_ip(ip_raw):
"""
IP address conversion to human readable format
:param ip_raw:
:return:
"""
ip = netaddr.IPAddress(ip_raw)
return ip
def integrate_devices(self):
"""
Fetch devices from phpipam and send them to upload function
:return:
"""
if not self.con:
self.connect()
with self.con:
cur = self.con.cursor()
q = '''
SELECT devices.hostname, devices.ip_addr, devices.description,
devices.type, devices.vendor, devices.model, deviceTypes.tname
FROM devices LEFT JOIN deviceTypes ON devices.type = deviceTypes.tid
'''
cur.execute(q)
db_devs = cur.fetchall()
if conf.DEBUG:
msg = ('Devices', str(db_devs))
logger.debugger(msg)
for line in db_devs:
dev = {}
ip = {}
dev_name, dev_ip, dev_description, dev_type_id, dev_vendor, dev_model, dev_type_name = line
dev.update({'name': dev_name})
dev.update({'manufacturer': dev_vendor})
dev.update({'hardware': dev_model})
if dev_type_name is not None and dev_type_name.lower() in dev_types:
dev.update({'type': dev_type_name.lower()})
rest.post_device(dev)
# send additional ip
ip.update({'ipaddress': dev_ip})
ip.update({'device': dev_name})
if dev_description is not None:
ip.update({'label': dev_description})
rest.post_ip(ip)
def integrate_vrfs(self):
"""
Fetch vrfs from phpipam and send them to upload function
:return:
"""
if not self.con:
self.connect()
with self.con:
cur = self.con.cursor()
q = "SELECT vrf.name, vrf.description FROM vrf"
cur.execute(q)
db_vrfs = cur.fetchall()
if conf.DEBUG:
msg = ('VRFs', str(db_vrfs))
logger.debugger(msg)
for line in db_vrfs:
vrf = {}
name, description = line
vrf.update({'name': name})
vrf.update({'description': description})
rest.post_vrf(vrf)
vrf = {}
vrf.update({'name': self.default_vrf_group})
vrf.update({'description': 'default_vrf'})
res = rest.post_vrf(vrf)
self.default_vrf_group_id = res['msg'][1]
def integrate_vlans(self):
"""
Fetch vlans from phpipam and send them to upload function
:return:
"""
if not self.con:
self.connect()
with self.con:
cur = self.con.cursor()
q = "SELECT vlans.name, vlans.number, vlans.description FROM vlans"
cur.execute(q)
db_vlans = cur.fetchall()
if conf.DEBUG:
msg = ('VLANs', str(db_vlans))
logger.debugger(msg)
for line in db_vlans:
vlan = {}
name, number, description = line
vlan.update({'name': name})
vlan.update({'number': number})
vlan.update({'description': description})
rest.post_vlan(vlan)
def integrate_subnets(self):
"""
Fetch subnets from phpipam and send them to upload function
:return:
"""
if not self.con:
self.connect()
with self.con:
cur = self.con.cursor()
q = '''
SELECT subnets.subnet, subnets.mask, subnets.description,
subnets.vlanId, subnets.masterSubnetId, vrf.name, vlans.number
FROM subnets LEFT JOIN vrf ON subnets.vrfId = vrf.vrfId
LEFT JOIN vlans ON subnets.vlanId = vlans.vlanId
WHERE subnets.isFolder = 0
'''
cur.execute(q)
subnets = cur.fetchall()
if conf.DEBUG:
msg = ('Subnets', str(subnets))
logger.debugger(msg)
rest_vrfs = json.loads(rest.get_vrfs())
rest_vlans = json.loads(rest.get_vlans())
rest_subnets = json.loads(rest.get_subnets())
for line in subnets:
sub = {}
raw_subnet, mask, description, parent_vlan, parent_subnet, vrf, vlan_number = line
subnet = self.convert_ip(int(raw_subnet))
sub.update({'network': subnet})
sub.update({'mask_bits': str(mask)})
sub.update({'name': description})
if vrf is not None:
# assign vrf group
for rest_vrf in rest_vrfs:
if rest_vrf['name'] == vrf:
sub.update({'vrf_group_id': rest_vrf['id']})
break
else:
sub.update({'vrf_group_id': self.default_vrf_group_id})
if parent_subnet is not 0:
# assign parent subnet
for rest_subnet in rest_subnets['subnets']:
if rest_subnet['name'] == subnet:
sub.update({'parent_subnet_id': rest_subnet['subnet_id']})
break
if parent_vlan is not 0:
# assign parent vlan
for rest_vlan in rest_vlans['vlans']:
if rest_vlan['number'] == vlan_number:
sub.update({'parent_vlan_id': rest_vlan['vlan_id']})
break
rest.post_subnet(sub)
def integrate_ips(self):
"""
Fetch ips from phpipam and send them to upload function
:return:
"""
if not self.con:
self.connect()
with self.con:
cur = self.con.cursor()
q = '''
SELECT ipaddresses.ip_addr, ipaddresses.description, ipaddresses.mac,
ipaddresses.lastSeen, subnets.subnet, vrf.name, devices.hostname, ipTags.type
FROM ipaddresses LEFT JOIN subnets ON ipaddresses.subnetId = subnets.id
LEFT JOIN vrf ON subnets.vrfId = vrf.vrfId
LEFT JOIN devices ON ipaddresses.switch = devices.id
LEFT JOIN ipTags ON ipaddresses.state = ipTags.id
'''
cur.execute(q)
ips = cur.fetchall()
if conf.DEBUG:
msg = ('IPs', str(ips))
logger.debugger(msg)
for line in ips:
address = {}
ip_raw, label, mac, last_seen, subnet, vrf, device, ip_type = line
subnet = self.convert_ip(int(subnet))
ip = self.convert_ip(int(ip_raw))
address.update({'ipaddress': ip})
address.update({'label': label})
address.update({'subnet': subnet})
if ip_type.lower() in ip_types:
address.update({'type': ip_type.lower()})
if last_seen is not None:
address.update({'available': 'yes'})
if device is not None:
address.update({'device': device})
if vrf is not None:
address.update({'vrf_group': vrf})
if mac is not None:
address.update({'macaddress': mac})
rest.post_ip(address)
def main():
db = DB()
db.integrate_devices()
db.integrate_vrfs()
db.integrate_vlans()
db.integrate_subnets()
db.integrate_ips()
if __name__ == '__main__':
logger = Logger(conf.LOGFILE, conf.STDOUT)
rest = REST()
main()
print '\n[!] Done!'
sys.exit()