Add an option to generate all DNS zone files specified in configuration
This commit is contained in:
parent
7d9f05445a
commit
0451515d07
2 changed files with 36 additions and 2 deletions
|
@ -1,3 +1,13 @@
|
|||
netbox:
|
||||
url: 'https://netbox.local'
|
||||
token: ''
|
||||
zones:
|
||||
- type: 'reverse'
|
||||
name: '192.0.2.0/24'
|
||||
file: '/path/to/file/db.192.0.2'
|
||||
- type: 'reverse'
|
||||
name: '2001:db8::/32'
|
||||
file: '/path/to/file/db'2001:db8'
|
||||
- type: 'forward'
|
||||
name: 'example.com'
|
||||
file: '/path/to/file/example.com'
|
||||
|
|
28
exporter.py
28
exporter.py
|
@ -83,7 +83,13 @@ def ptr(nb, args):
|
|||
env = Environment(loader=file_loader)
|
||||
template = env.get_template("zonefile.jj2")
|
||||
output = template.render(serial=serial,records=records)
|
||||
print(output)
|
||||
|
||||
if not hasattr(args, 'output'):
|
||||
print(output)
|
||||
else:
|
||||
f = open(args.output,'w')
|
||||
f.write(output)
|
||||
f.close()
|
||||
|
||||
def dns(nb, args):
|
||||
devices_id = []
|
||||
|
@ -162,7 +168,23 @@ def dns(nb, args):
|
|||
env = Environment(loader=file_loader)
|
||||
template = env.get_template("zonefile.jj2")
|
||||
output = template.render(serial=serial,records=records)
|
||||
print(output)
|
||||
|
||||
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'],output=zone['file']))
|
||||
elif zone['type'] == 'forward':
|
||||
dns(nb, argparse.Namespace(domain=zone['name'],output=zone['file']))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -174,6 +196,8 @@ if __name__ == '__main__':
|
|||
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')
|
||||
|
||||
|
|
Loading…
Reference in a new issue