def handle(self, filename, *args, **options): zone = dns.zone.from_file(filename) domain = Domain(name=str(zone.origin).strip('.'), type='MASTER') domain.save() for entry in zone: node = zone[entry] name = str(entry.choose_relativity(zone.origin, False)).strip('.') for rdataset in node: qtype = dns.rdatatype.to_text(rdataset.rdtype) ttl = rdataset.ttl for rdata in rdataset: prio = None if qtype == 'MX': prio = rdata.preference content = str( rdata.exchange.choose_relativity( zone.origin, False)) elif qtype == 'TXT': content = b'\n'.join(rdata.strings) elif qtype == 'CNAME': content = rdata.to_text(zone.origin, False).strip('.') else: content = rdata.to_text(zone.origin, False) record = Record( domain=domain, name=name, type=qtype, ttl=ttl, prio=prio, content=content, ) record.save()
def handle(self, filename, *args, **options): zone = dns.zone.from_file(filename) domain = Domain(name=str(zone.origin).strip('.'), type='MASTER') domain.save() for entry in zone: node = zone[entry] name = str(entry.choose_relativity(zone.origin, False)).strip('.') for rdataset in node: qtype = dns.rdatatype.to_text(rdataset.rdtype) ttl = rdataset.ttl for rdata in rdataset: prio = None if qtype == 'MX': prio = rdata.preference content = str(rdata.exchange.choose_relativity( zone.origin, False)) elif qtype == 'TXT': content = b'\n'.join(rdata.strings) elif qtype == 'CNAME': content = rdata.to_text(zone.origin, False).strip('.') else: content = rdata.to_text(zone.origin, False) record = Record( domain=domain, name=name, type=qtype, ttl=ttl, prio=prio, content=content, ) record.save()
def handle_single(self, filename): print('Importing DNS records from {}...'.format(filename)) zone = dns.zone.from_file(filename) domain, _ = Domain.objects.get_or_create( name=str(zone.origin).strip('.'), type='MASTER', ) domain.save() for entry in zone: node = zone[entry] name = str(entry.choose_relativity(zone.origin, False)).strip('.') for rdataset in node: qtype = dns.rdatatype.to_text(rdataset.rdtype) ttl = rdataset.ttl for rdata in rdataset: prio = None if qtype == 'MX': prio = rdata.preference content = str(rdata.exchange.choose_relativity( zone.origin, False)) elif qtype == 'TXT': content = b'\n'.join(rdata.strings) elif qtype == 'CNAME': content = rdata.to_text(zone.origin, False).strip('.') else: content = rdata.to_text(zone.origin, False) record = Record( domain=domain, name=name, type=qtype, ttl=ttl, prio=prio, content=content, ) record.save()
def set_txt_record(domain, name, title, value): """Updates or creates a TXT record with the given title.""" try: record = Record.objects.get(type="TXT", name=name, domain=domain, content__startswith=title + ": ") except Record.DoesNotExist: record = Record(name=name, type="TXT", domain=domain) record.content = "%s: %s" % (title, value) record.save()
def _dns_create_record(form, request, device): if form.cleaned_data.get('dns_new_content'): record = Record() _dns_fill_record(form, 'dns_new_', record, request) record.saving_user = request.user record.saving_device = device record.save() messages.success(request, "A DNS record added.")
def set_txt_record(domain, name, title, value): """Updates or creates a TXT record with the given title.""" try: record = Record.objects.get( type='TXT', name=name, domain=domain, content__startswith=title + ': ', ) except Record.DoesNotExist: record = Record(name=name, type='TXT', domain=domain) record.content = '%s: %s' % (title, value) record.save()
def add_dns_address(name, ip): """Add a new DNS record in the right domain.""" name = name.strip().strip('.') ip = str(ip).strip().strip('.') host_name, domain_name = name.split('.', 1) domain = Domain.objects.get(name=domain_name) record = Record( domain=domain, name=name, type='A', content=ip, ) record.save()
def set_revdns_record(ip, name, ttl=None, prio=None, overwrite=False, create=False): revname = '.'.join(reversed(ip.split('.'))) + '.in-addr.arpa' domain_name = '.'.join(list(reversed(ip.split('.')))[1:]) + '.in-addr.arpa' if not create: try: domain = Domain.objects.get(name=domain_name) except Domain.DoesNotExist: raise RevDNSNoDomain('Domain %s not found.' % domain_name) else: domain, created = Domain.objects.get_or_create(name=domain_name) records = Record.objects.filter(name=revname, type='PTR') creating = True for record in records: creating = False if not overwrite and record.content != name: raise RevDNSExists('RevDNS record for %s already exists.' % revname) if creating: records = [Record(name=revname, type='PTR')] for record in records: record.content = name record.domain = domain if ttl is not None: record.ttl = ttl if prio is not None: record.prio = prio record.save() return create
def test_record_create(self): record = Record( name='hostname.example.com', type='A', content='127.0.0.1', domain=self.domain, ) record.saving_user = self.user record.save() history = DNSHistory.objects.get( record_name='hostname.example.com', field_name='name', user=self.user, ) self.assertEqual(history.old_value, '') self.assertEqual(history.new_value, 'hostname.exmaple.com') history = DNSHistory.objects.get( record_name='hostname.example.com', field_name='content', user=self.user, ) self.assertEqual(history.old_value, '') self.assertEqual(history.new_value, '127.0.0.1')
def handle_single(self, filename): print('Importing DNS records from {}...'.format(filename)) zone = dns.zone.from_file(filename) domain, _ = Domain.objects.get_or_create( name=str(zone.origin).strip('.'), type='MASTER', ) domain.save() for entry in zone: node = zone[entry] name = str(entry.choose_relativity(zone.origin, False)).strip('.') for rdataset in node: qtype = dns.rdatatype.to_text(rdataset.rdtype) ttl = rdataset.ttl for rdata in rdataset: prio = None if qtype == 'MX': prio = rdata.preference content = str( rdata.exchange.choose_relativity( zone.origin, False)) elif qtype == 'TXT': content = b'\n'.join(rdata.strings) elif qtype == 'CNAME': content = rdata.to_text(zone.origin, False).strip('.') else: content = rdata.to_text(zone.origin, False) record = Record( domain=domain, name=name, type=qtype, ttl=ttl, prio=prio, content=content, ) record.save()