def updateHostIp(self, host, ip): map = self.map record = map.get(host) if not record: raise RuntimeError("Invalid host name") for r in record: file = r['file'] old_ip = r['ip'] zone = dns.zone.from_file(file, os.path.basename(file),relativize=False) for k, v in zone.iteritems(): for dataset in v.rdatasets: for item in dataset.items: if hasattr(item, 'address') and item.address == old_ip: item.address = ip zone.to_file(file) self.restart()
def updateHostIp(self, host, ip): map = self.map record = map.get(host) if not record: raise RuntimeError("Invalid host name") for r in record: file = r['file'] old_ip = r['ip'] zone = dns.zone.from_file(file, os.path.basename(file), relativize=False) for k, v in zone.iteritems(): for dataset in v.rdatasets: for item in dataset.items: if hasattr(item, 'address') and item.address == old_ip: item.address = ip zone.to_file(file) self.restart()
def cmd_rrlist(args, r53): zone = _get_records(args, r53) print '\t'.join(["host", "ttl", "cls", "type", "data"]) for record_name, record_value in zone.iteritems(): print '\t'.join(record_value.to_text(record_name).split(' '))
def build_json(self, dns_zones, output): """Converts zone transfer results from list to JSON files. Converts the zone transfer results from a list of tuples with domain and dns zones into JSON unicode output. One file is written to disk per domain. Args: dns_zones (list): List of tuples with domain and zone transfer. output (str): Path to output file. Raises: ValueError: If dns_zones are empty. ValueError: If output file is not a file. """ if dns_zones == None or dns_zones == "": error_msg = u'Received no results to write too file.' raise ValueError(error_msg) if os.path.isdir(output): error_msg = u'Output file is a directory, needs filename.' raise ValueError(error_msg) for domain, zone in dns_zones: file_name, ext = os.path.splitext(output) output_ext = file_name + '-' + domain + '-dns' + ext f = codecs.open(output_ext, "w", "utf-8") os.chmod(output_ext, 0440) for name, node in zone.iteritems(): rdatasets = node.rdatasets name = unicode(name) json_output = {} current_time = unicode(time.mktime(time.localtime())) json_output['extractTime'] = current_time json_output['datasource'] = self.DOMAINS_DICT[domain]['source'] json_output['datasource_type'] = self.DOMAINS_DICT[domain]['source_type'] json_output['datasource_value'] = self.DOMAINS_DICT[domain]['source_value'] json_output['name'] = name json_output['domain'] = domain if name != '@': json_output['fqdn'] = name + u'.' + domain rdata_output = [] for rdataset in rdatasets: rdclass = dns.rdataclass.to_text(rdataset.rdclass) rdtype = dns.rdatatype.to_text(rdataset.rdtype) ttl = rdataset.ttl rdata_json = {} rdata_json['class'] = unicode(rdclass) rdata_json['type'] = unicode(rdtype) rdata_json['ttl'] = unicode(ttl) if rdtype == 'A' or rdtype == 'AAAA': rdata_json['rdata'] = self.parse_a_record(rdataset) elif rdtype == 'MX': rdata_json['rdata'] = self.parse_mx_record(rdataset) elif rdtype == 'SOA': rdata_json['rdata'] = self.parse_soa_record(rdataset) elif rdtype == 'NAPTR': rdata_json['rdata'] = self.parse_naptr_record(rdataset) elif rdtype == 'TXT' or rdtype == 'SPF': rdata_json['rdata'] = self.parse_txt_record(rdataset) elif rdtype == 'CNAME': rdata_json['rdata'] = self.parse_cname_record(rdataset) elif rdtype == 'SRV': rdata_json['rdata'] = self.parse_srv_record(rdataset) elif rdtype == 'NS': rdata_json['rdata'] = self.parse_ns_record(rdataset) elif rdtype == 'SSHFP': rdata_json['rdata'] = self.parse_sshfp_record(rdataset) else: error_msg = u'Record type ' + unicode(rdtype) + \ u' not supported. Please revise script.\n' print(error_msg) rdata_output.append(rdata_json) json_output['rdatasets'] = rdata_output json.dump(json_output, f, indent=4, separators=(',', ': '), ensure_ascii=False, sort_keys=True) f.write('\n') f.close()