Skip to content

Instantly share code, notes, and snippets.

@kljensen
Forked from dlage/README.md
Last active March 8, 2021 11:56
Show Gist options
  • Save kljensen/382cd8166c4b6f68ac96ffcad0a4e02d to your computer and use it in GitHub Desktop.
Save kljensen/382cd8166c4b6f68ac96ffcad0a4e02d 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):
domain = dns_info['Result']['DomainBasicDetails']['DomainName']
records = dns_info['Result']['CustomHostRecords']['Records']
items = []
record_types = {
1: 'A',
2: 'CNAME',
3: 'MX',
5: 'TXT',
8: 'AAAA'
}
for record in records:
host = str(record['Host'])
if host == '@':
host = domain
else:
host = host + '.' + domain
if record['RecordType'] in record_types.keys():
tipe = record_types[record['RecordType']]
else:
# Skipping unknown record
tipe = 'UNKNOWN'
host = "# Record not identified:\n# " + host
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment