Commit 8e75f743 authored by aoh's avatar aoh
Browse files

generator.py: Use nameserver for ip look up


Signed-off-by: aoh's avataraoh <aoh@lysator.liu.se>
parent 9abe6be6
#!/usr/bin/env python3
from jinja2 import FileSystemLoader, Environment
from db import DB
from socket import gethostbyname
from dns.resolver import Resolver
# Represents the client key for updating a DNS record.
class Key:
......@@ -40,6 +42,21 @@ class Record:
self.type = type
self.address = address
# Resolve fully qualified domain name
def lookup(fully_qualified_domain_name):
resolver = Resolver()
resolver.nameservers=[gethostbyname('resolver.lysator.liu.se')]
ips = list()
try:
ips += [str(x) for x in resolver.query(fully_qualified_domain_name, 'A')]
except:
pass
try:
ips += [str(x) for x in resolver.query(fully_qualified_domain_name, 'AAAA')]
except:
pass
return ips
if __name__ == '__main__':
env = Environment(loader = FileSystemLoader('.'))
......@@ -76,7 +93,6 @@ if __name__ == '__main__':
print(rendered_zone)
# Zonefile
ip = '10.0.2.2' # TODO obtain correct ip
with DB() as d:
for id in d.get_domain_ids():
domain_name = d.get_domain_name(id)
......@@ -84,11 +100,16 @@ if __name__ == '__main__':
records = list()
for sid in d.get_subdomain_ids(id):
rr = d.get_record(sid)
record = Record(rr.get('name'),
rr.get('ttl'),
rr.get('record_types.name'),
ip)
records.append(record)
subdomain_name = d.get_subdomain_name(sid)
fqdn = subdomain_name + '.' + domain_name
if subdomain_name == "@":
fqdn = domain_name
for ip in lookup(fqdn):
record = Record(rr.get('name'),
rr.get('ttl'),
rr.get('record_types.name'),
ip)
records.append(record)
zonefile = ZoneFile(domain_name,
'dns.lysator.liu.se',
'dns.lysator.liu.se',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment