-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexporter.py
More file actions
executable file
·247 lines (186 loc) · 9.4 KB
/
Copy pathexporter.py
File metadata and controls
executable file
·247 lines (186 loc) · 9.4 KB
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
#!/usr/bin/env python3
import argparse
import ipaddress
import os
import pynetbox
import sys
import yaml
import re
from datetime import datetime
from jinja2 import Environment, FileSystemLoader
config = os.path.join(os.path.dirname(os.path.realpath(__file__)),'config.yml')
with open(config, 'r') as ymlfile:
cfg = yaml.load(ymlfile, Loader=yaml.FullLoader)
templates_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),'templates')
def ptr(nb, args):
device_primary = {}
device_cluster = {}
vm_primary = {}
records = {}
extra_records = []
serial = 0
af = ipaddress.ip_network(args.prefix).version
extras = nb.extras.config_contexts.get(name=args.prefix)
devices = nb.dcim.devices.all()
vms = nb.virtualization.virtual_machines.filter(has_primary_ip=True)
addresses = nb.ipam.ip_addresses.filter(parent=args.prefix)
if extras is not None:
extra_records = extras.data['records']
for device in devices:
last_updated = int(datetime.timestamp(datetime.strptime(device.last_updated, '%Y-%m-%dT%H:%M:%S.%f%z')))
if last_updated > serial: serial = last_updated
if device.virtual_chassis:
device_cluster[device.id] = device.virtual_chassis.name
if af == 4 and device.primary_ip4:
device_primary[device.id] = device.primary_ip4.id
elif af == 6 and device.primary_ip6:
device_primary[device.id] = device.primary_ip6.id
for vm in vms:
last_updated = int(datetime.timestamp(datetime.strptime(vm.last_updated, '%Y-%m-%dT%H:%M:%S.%f%z')))
if last_updated > serial: serial = last_updated
if af == 4 and vm.primary_ip4:
vm_primary[vm.id] = vm.primary_ip4.id
elif af == 6 and vm.primary_ip6:
vm_primary[vm.id] = vm.primary_ip6.id
for address in addresses:
last_updated = int(datetime.timestamp(datetime.strptime(address.last_updated, '%Y-%m-%dT%H:%M:%S.%f%z')))
if last_updated > serial: serial = last_updated
ip = ipaddress.ip_interface(address.address).ip
ptr = ipaddress.ip_address(ip).reverse_pointer
if address.dns_name:
records[ptr] = [{"type":"PTR","rr":address.dns_name}]
elif address.assigned_object_type == 'dcim.interface':
if address.assigned_object.device.id in device_cluster:
address.assigned_object.device.name = device_cluster[address.assigned_object.device.id]
if address.assigned_object.device.name is not None:
if address.assigned_object.device.id in device_primary and address.id == device_primary[address.assigned_object.device.id]:
records[ptr] = [{"type":"PTR","rr":address.assigned_object.device.name}]
else:
iname = re.sub(r'[^a-zA-Z0-9]', '-',address.assigned_object.name).lower()
records[ptr] = [{"type":"PTR","rr":".".join((iname,address.assigned_object.device.name))}]
elif address.assigned_object_type == 'virtualization.vminterface':
if address.assigned_object.virtual_machine.id in vm_primary and address.id == vm_primary[address.assigned_object.virtual_machine.id]:
records[ptr] = [{"type":"PTR","rr":address.assigned_object.virtual_machine.name}]
else:
iname = re.sub(r'[^a-zA-Z0-9]', '-',address.assigned_object.name).lower()
records[ptr] = [{"type":"PTR","rr":".".join((iname,address.assigned_object.virtual_machine.name))}]
file_loader = FileSystemLoader(templates_dir)
env = Environment(loader=file_loader)
template = env.get_template(args.template)
output = template.render(serial=serial,records=records, extra_records=extra_records)
if not hasattr(args, 'output'):
print(output)
else:
f = open(args.output,'w')
f.write(output)
f.close()
def dns(nb, args):
devices_id = []
clusters_id = []
vm_id = []
primary_ip = {}
vc_devices = {}
records = {}
extra_records = []
serial = 0
extras = nb.extras.config_contexts.get(name=args.domain)
devices = nb.dcim.devices.filter(name__iew=args.domain)
clusters = nb.dcim.virtual_chassis.filter(domain=args.domain)
cluster_devices = nb.dcim.devices.filter(virtual_chassis_member=True)
vms = nb.virtualization.virtual_machines.filter(name__iew=args.domain)
addresses = nb.ipam.ip_addresses.all()
if extras is not None:
extra_records = extras.data['records']
for cluster in clusters:
clusters_id.append(cluster.id)
for device in devices:
last_updated = int(datetime.timestamp(datetime.strptime(device.last_updated, '%Y-%m-%dT%H:%M:%S.%f%z')))
if last_updated > serial: serial = last_updated
devices_id.append(device.id)
if device.primary_ip4:
primary_ip[device.primary_ip4.id] = device.name
if device.primary_ip6:
primary_ip[device.primary_ip6.id] = device.name
for cluster_device in cluster_devices:
if cluster_device.virtual_chassis.id in clusters_id:
last_updated = int(datetime.timestamp(datetime.strptime(cluster_device.last_updated, '%Y-%m-%dT%H:%M:%S.%f%z')))
if last_updated > serial: serial = last_updated
devices_id.append(cluster_device.id)
vc_devices[cluster_device.id] = cluster_device.virtual_chassis.name
if cluster_device.primary_ip4:
primary_ip[cluster_device.primary_ip4.id] = cluster_device.virtual_chassis.name
if cluster_device.primary_ip6:
primary_ip[cluster_device.primary_ip6.id] = cluster_device.virtual_chassis.name
for vm in vms:
last_updated = int(datetime.timestamp(datetime.strptime(vm.last_updated, '%Y-%m-%dT%H:%M:%S.%f%z')))
if last_updated > serial: serial = last_updated
vm_id.append(vm.id)
if vm.primary_ip4:
primary_ip[vm.primary_ip4.id] = vm.name
if vm.primary_ip6:
primary_ip[vm.primary_ip6.id] = vm.name
for address in addresses:
last_updated = int(datetime.timestamp(datetime.strptime(address.last_updated, '%Y-%m-%dT%H:%M:%S.%f%z')))
if last_updated > serial: serial = last_updated
ip = ipaddress.ip_interface(address.address).ip
if ipaddress.ip_address(ip).version == 4:
type = "A"
else:
type = "AAAA"
if address.dns_name and address.dns_name.endswith(args.domain):
if address.dns_name not in records:
records[address.dns_name] = []
records[address.dns_name].append({"type":type,"rr":ip})
elif address.id in primary_ip:
if primary_ip[address.id] not in records:
records[primary_ip[address.id]] = []
records[primary_ip[address.id]].append({"type":type,"rr":ip})
elif address.assigned_object_type == 'dcim.interface' and address.assigned_object.device.id in devices_id:
iname = re.sub(r'[^a-zA-Z0-9]', '-',address.assigned_object.name).lower()
if address.assigned_object.device.id in vc_devices:
fname = ".".join((iname,vc_devices[address.assigned_object.device.id]))
else:
fname = ".".join((iname,address.assigned_object.device.name))
if fname not in records:
records[fname] = []
records[fname].append({"type":type,"rr":ip})
elif address.assigned_object_type == 'virtualization.vminterface' and address.assigned_object.virtual_machine.id in vm_id:
iname = re.sub(r'[^a-zA-Z0-9]', '-',address.assigned_object.name).lower()
fname = ".".join((iname,address.assigned_object.virtual_machine.name))
if fname not in records:
records[fname] = []
records[fname].append({"type":type,"rr":ip})
file_loader = FileSystemLoader(templates_dir)
env = Environment(loader=file_loader)
template = env.get_template(args.template)
output = template.render(serial=serial,records=records,extra_records=extra_records)
if not hasattr(args, 'output'):
print(output)
else:
f = open(args.output,'w')
f.write(output)
f.close()
def autodns(nb, args):
if not 'zones' in cfg:
return
for zone in cfg['zones']:
if zone['type'] == 'reverse':
ptr(nb, argparse.Namespace(prefix=zone['name'],template=zone['template'],output=zone['file']))
elif zone['type'] == 'forward':
dns(nb, argparse.Namespace(domain=zone['name'],template=zone['template'],output=zone['file']))
if __name__ == '__main__':
nb = pynetbox.api(
cfg['netbox']['url'],
token=cfg['netbox']['token']
)
parser = argparse.ArgumentParser(description='Netbox API exporter')
subparsers = parser.add_subparsers(help='Action to perform',dest='action',required=True)
subparser = subparsers.add_parser('autodns', help='Generate all DNS zone files using configuration')
subparser = subparsers.add_parser('ptr', help='Generate reverse DNS zone file for prefix')
subparser.add_argument('prefix', type=str, help='Prefix')
subparser.add_argument('template', type=str, help='template')
subparser = subparsers.add_parser('dns', help='Generate DNS zone file for domain')
subparser.add_argument('domain', type=str, help='Domain')
subparser.add_argument('template', type=str, help='template')
args = parser.parse_args()
globals()[args.action](nb, args)