Created
May 18, 2022 12:01
-
-
Save wido/c7f2c4445106fc498477de87b1975233 to your computer and use it in GitHub Desktop.
Parse DNS export from CloudFlare to import into FreeIPA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import argparse | |
def parse_line(line): | |
if not line: | |
return | |
if line[0] == ';': | |
return | |
return line.split(maxsplit=4) | |
def ipa_record_commands(zone, records): | |
for record in records: | |
rname = record[0] | |
rttl = record[1] | |
rtype = record[3] | |
rcontent = record[4] | |
command = "ipa dnsrecord-add {0} {1} --ttl={2} ".format(zone, rname, rttl) | |
if type == 'SOA': | |
return | |
elif rtype == 'MX': | |
prio, content = rcontent.split() | |
command += "--mx-exchanger={0} --mx-preference={1}".format(content, prio) | |
else: | |
if rcontent[0] == '"': | |
content = rcontent | |
else: | |
content = "\"{}\"".format(rcontent) | |
command += "--{0}-rec={1}".format(rtype.lower(), content) | |
print(command) | |
def read_cloudflare_dns_export(filename, zone): | |
records = [] | |
with open(filename) as file: | |
for line in file: | |
record = parse_line(line.rstrip()) | |
if record: | |
records.append(record) | |
ipa_record_commands(zone, records) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Read CloudFlare DNS export and print FreeIPA commands') | |
parser.add_argument('filename', help='path to CloudFlare export') | |
parser.add_argument('--zone', help='New zone to import these records into') | |
args = parser.parse_args() | |
read_cloudflare_dns_export(args.filename, args.zone) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment