forked from Cray-HPE/cray-dhcp-kea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_network_cidr.py
28 lines (26 loc) · 1.19 KB
/
get_network_cidr.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
#!/usr/bin/env python
import requests
import json
import ipaddress
import os
resp = requests.get(url='http://cray-sls/v1/search/hardware?type=comptype_cabinet')
subnet4 = []
for i in resp.json():
for item in resp.json()[0]['ExtraProperties']['Networks']['cn']:
subnet4_subnet = {}
subnet4_subnet['pools'] = {}
subnet4_subnet['pools']['pool'] = {}
subnet4_subnet['option-data'] = {}
cn_network_cidr = resp.json()[0]['ExtraProperties']['Networks']['cn'][item]['CIDR']
cn_gateway = resp.json()[0]['ExtraProperties']['Networks']['cn'][item]['Gateway']
cn_network = ipaddress.ip_network(cn_network_cidr)
cn_network_total_hosts = cn_network.num_addresses
cn_network_pool_start = cn_network[26]
cn_network_pool_end = cn_network[cn_network_total_hosts - 51]
# create dictionary json for subnet
subnet4_subnet['subnet'] = cn_network_cidr
subnet4_subnet['pools']['pool'] = str(cn_network_pool_start) + '-' + str(cn_network_pool_end)
subnet4_subnet['option-data']['name'] = 'router'
subnet4_subnet['option-data']['data'] = cn_gateway
subnet4.append(subnet4_subnet)
print (json.dumps(subnet4))