netbox-exporter/exporter.py

249 lines
9.4 KiB
Python
Raw Normal View History

2021-04-08 18:27:54 +00:00
#!/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)
2021-04-11 08:24:41 +00:00
templates_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),'templates')
2021-04-08 18:27:54 +00:00
def ptr(nb, args):
device_primary = {}
2021-04-09 15:34:06 +00:00
device_cluster = {}
2021-04-08 18:27:54 +00:00
vm_primary = {}
records = {}
2022-11-11 22:31:43 +00:00
extra_records = []
2021-04-08 18:27:54 +00:00
serial = 0
af = ipaddress.ip_network(args.prefix).version
2022-11-11 22:31:43 +00:00
extras = nb.extras.config_contexts.get(name=args.prefix)
2021-04-09 15:34:06 +00:00
devices = nb.dcim.devices.all()
2021-04-08 18:27:54 +00:00
vms = nb.virtualization.virtual_machines.filter(has_primary_ip=True)
addresses = nb.ipam.ip_addresses.filter(parent=args.prefix)
2022-11-11 22:31:43 +00:00
if extras is not None:
extra_records = extras.data['records']
2021-04-08 18:27:54 +00:00
for device in devices:
2022-11-11 22:31:43 +00:00
last_updated = int(datetime.timestamp(datetime.strptime(device.last_updated, '%Y-%m-%dT%H:%M:%S.%f%z')))
2021-04-08 18:27:54 +00:00
if last_updated > serial: serial = last_updated
2021-04-09 15:34:06 +00:00
if device.virtual_chassis:
device_cluster[device.id] = device.virtual_chassis.name
2021-04-08 18:27:54 +00:00
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:
2022-11-11 22:31:43 +00:00
last_updated = int(datetime.timestamp(datetime.strptime(vm.last_updated, '%Y-%m-%dT%H:%M:%S.%f%z')))
2021-04-08 18:27:54 +00:00
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:
2022-11-11 22:31:43 +00:00
last_updated = int(datetime.timestamp(datetime.strptime(address.last_updated, '%Y-%m-%dT%H:%M:%S.%f%z')))
2021-04-08 18:27:54 +00:00
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':
2021-04-09 15:34:06 +00:00
if address.assigned_object.device.id in device_cluster:
address.assigned_object.device.name = device_cluster[address.assigned_object.device.id]
2021-05-30 06:59:20 +00:00
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:
2022-11-11 22:31:43 +00:00
iname = re.sub(r'[^a-zA-Z0-9]', '-',address.assigned_object.name).lower()
2021-05-30 06:59:20 +00:00
records[ptr] = [{"type":"PTR","rr":".".join((iname,address.assigned_object.device.name))}]
2021-04-08 18:27:54 +00:00
elif address.assigned_object_type == 'virtualization.vminterface':
2021-04-09 15:34:06 +00:00
if address.assigned_object.virtual_machine.id in vm_primary and address.id == vm_primary[address.assigned_object.virtual_machine.id]:
2021-04-08 18:27:54 +00:00
records[ptr] = [{"type":"PTR","rr":address.assigned_object.virtual_machine.name}]
else:
2022-11-11 22:31:43 +00:00
iname = re.sub(r'[^a-zA-Z0-9]', '-',address.assigned_object.name).lower()
2021-04-08 18:27:54 +00:00
records[ptr] = [{"type":"PTR","rr":".".join((iname,address.assigned_object.virtual_machine.name))}]
file_loader = FileSystemLoader(templates_dir)
env = Environment(loader=file_loader)
2021-04-11 08:24:41 +00:00
template = env.get_template(args.template)
2022-11-11 22:31:43 +00:00
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()
2021-04-08 18:27:54 +00:00
def dns(nb, args):
devices_id = []
2021-05-04 20:01:14 +00:00
clusters_id = []
2021-04-08 18:27:54 +00:00
vm_id = []
primary_ip = {}
2021-05-04 20:01:14 +00:00
vc_devices = {}
2021-04-08 18:27:54 +00:00
records = {}
2022-11-11 22:36:33 +00:00
extra_records = []
2021-04-08 18:27:54 +00:00
serial = 0
2022-11-11 22:31:43 +00:00
extras = nb.extras.config_contexts.get(name=args.domain)
2021-04-08 18:27:54 +00:00
devices = nb.dcim.devices.filter(name__iew=args.domain)
2021-05-04 20:01:14 +00:00
clusters = nb.dcim.virtual_chassis.filter(domain=args.domain)
cluster_devices = nb.dcim.devices.filter(virtual_chassis_member=True)
2021-04-08 18:27:54 +00:00
vms = nb.virtualization.virtual_machines.filter(name__iew=args.domain)
addresses = nb.ipam.ip_addresses.all()
2022-11-11 22:31:43 +00:00
if extras is not None:
extra_records = extras.data['records']
2021-05-04 20:01:14 +00:00
for cluster in clusters:
clusters_id.append(cluster.id)
for cluster_device in cluster_devices:
if cluster_device.virtual_chassis.id in clusters_id:
2022-11-11 22:31:43 +00:00
last_updated = int(datetime.timestamp(datetime.strptime(cluster_device.last_updated, '%Y-%m-%dT%H:%M:%S.%f%z')))
2021-05-04 20:01:14 +00:00
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
2021-04-08 18:27:54 +00:00
for device in devices:
2022-11-11 22:31:43 +00:00
last_updated = int(datetime.timestamp(datetime.strptime(device.last_updated, '%Y-%m-%dT%H:%M:%S.%f%z')))
2021-04-08 18:27:54 +00:00
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 vm in vms:
2022-11-11 22:31:43 +00:00
last_updated = int(datetime.timestamp(datetime.strptime(vm.last_updated, '%Y-%m-%dT%H:%M:%S.%f%z')))
2021-04-08 18:27:54 +00:00
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:
2022-11-11 22:31:43 +00:00
last_updated = int(datetime.timestamp(datetime.strptime(address.last_updated, '%Y-%m-%dT%H:%M:%S.%f%z')))
2021-04-08 18:27:54 +00:00
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):
2021-04-09 15:34:06 +00:00
if address.dns_name not in records:
records[address.dns_name] = []
records[address.dns_name].append({"type":type,"rr":ip})
2021-04-08 18:27:54 +00:00
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:
2022-11-11 22:31:43 +00:00
iname = re.sub(r'[^a-zA-Z0-9]', '-',address.assigned_object.name).lower()
2021-05-04 20:01:14 +00:00
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))
2021-04-08 18:27:54 +00:00
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:
2022-11-11 22:31:43 +00:00
iname = re.sub(r'[^a-zA-Z0-9]', '-',address.assigned_object.name).lower()
2021-04-08 18:27:54 +00:00
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)
2021-04-11 08:24:41 +00:00
template = env.get_template(args.template)
2022-11-11 22:31:43 +00:00
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':
2021-04-11 08:24:41 +00:00
ptr(nb, argparse.Namespace(prefix=zone['name'],template=zone['template'],output=zone['file']))
elif zone['type'] == 'forward':
2021-04-11 08:24:41 +00:00
dns(nb, argparse.Namespace(domain=zone['name'],template=zone['template'],output=zone['file']))
2021-04-08 18:27:54 +00:00
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')
2021-04-08 18:27:54 +00:00
subparser = subparsers.add_parser('ptr', help='Generate reverse DNS zone file for prefix')
subparser.add_argument('prefix', type=str, help='Prefix')
2021-04-11 08:24:41 +00:00
subparser.add_argument('template', type=str, help='template')
2021-04-08 18:27:54 +00:00
subparser = subparsers.add_parser('dns', help='Generate DNS zone file for domain')
subparser.add_argument('domain', type=str, help='Domain')
2021-04-11 08:24:41 +00:00
subparser.add_argument('template', type=str, help='template')
2021-04-08 18:27:54 +00:00
args = parser.parse_args()
globals()[args.action](nb, args)