Skip to content

Instantly share code, notes, and snippets.

@andrew-nuwber
Last active December 29, 2023 00:26
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save andrew-nuwber/3bc955e94e667cb5e62cec6de57bbef9 to your computer and use it in GitHub Desktop.
Save andrew-nuwber/3bc955e94e667cb5e62cec6de57bbef9 to your computer and use it in GitHub Desktop.
Namecheap DNS to zone file
# Originally by Judotens Budiarto (https://github.com/judotens)
# See: https://gist.github.com/judotens/151341f04b37ffeb5b59
def parse_dns_info(dns_info):
records = dns_info['Result']['CustomHostRecords']['Records']
items = []
for record in records:
host = str(record['Host'])
if record['RecordType'] == 1: tipe = 'A'
if record['RecordType'] == 2: tipe = 'CNAME'
if record['RecordType'] == 3: tipe = 'MX'
if record['RecordType'] == 5: tipe = 'TXT'
value = str(record['Data'])
ttl = str(record['Ttl'])
priority = str(record['Priority'])
active = record['IsActive']
if not active: continue
new_value = value
if tipe == 'MX': new_value = "%s %s" % (str(priority), str(value))
if tipe == 'TXT': new_value = "\"%s\"" % (str(value))
items.append([host,ttl,"IN", tipe, new_value])
return items
if __name__ == "__main__":
import sys
import json
file_name = sys.argv[1]
try:
file = open(file_name, 'r')
except IOError:
print("File not accessible")
dns_info = json.loads(file.read());
zones = parse_dns_info(dns_info)
for zone in zones:
print "\t".join(zone)
@donPatino
Copy link

This worked! Thank you!
Only wish I could export my url redirects :/

@dlage
Copy link

dlage commented Dec 7, 2020

Thanks for the gist. I made some changes to make it easier to import in Cloudflare for example and also make sure we know what type of record we're exporting and still not omit any information.
Also, added support for AAAA records.
Edit: forgot to leave the link: https://gist.github.com/dlage/93b880c700081d8c0938978b9253c91b

@gregsadetsky
Copy link

Thank you, super useful!

@demonshreder
Copy link

Thanks, helped a lot

@praiseisaac
Copy link

Thank you

@howsonwhocodes
Copy link

Thanks, this was really useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment