-
Notifications
You must be signed in to change notification settings - Fork 0
/
ns1_zone_parser.py
65 lines (49 loc) · 2.25 KB
/
ns1_zone_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import json
import argparse
from datetime import datetime
def load_json(file):
try:
with open(file, 'r') as f:
return json.load(f)
except (FileNotFoundError, json.JSONDecodeError) as e:
print("Error occurred while reading the input file:", str(e))
exit()
def write_soa_record(file, data):
file.write(f';; Domain: {data["name"]}.\n')
file.write(f';; Exported: {datetime.fromtimestamp(data["created_at"])}\n\n')
file.write(f'$ORIGIN {data["name"]}\n')
file.write(f'$TTL {data["nx_ttl"]}\n')
def write_records(file, record_type, records, record_ttls):
if len(records) > 0:
file.write(f'\n;; {record_type} Records\n')
for domain, content in records.items():
content = content.rstrip('.')
answers = [answer for answer in content.split()]
ttl = record_ttls[record_type][domain]
if answers:
file.write(f"{domain}\t{ttl}\tIN\t{record_type}\t{answers[0] if len(answers) == 1 else ' '.join(answers)}.\n")
def main():
parser = argparse.ArgumentParser(description='Convert NS1 JSON zone file to DNS zone file.')
parser.add_argument('file', help='Input JSON file name')
args = parser.parse_args()
data = load_json(args.file)
zone_name = data['name']
records = data['records']
# Open the zone file
with open(f'{zone_name}.zone', 'w') as f:
write_soa_record(f, data)
record_types = {'CNAME': {}, 'A': {}, 'AAAA': {}, 'TXT': {}, 'MX': {}, 'SOA': {}, 'NS': {}}
record_ttls = {record_type: {} for record_type in record_types}
for record in records:
domain = record['domain']
type_ = record['type']
record_types[type_][domain] = record['short_answers'][0].split()[-1] if record.get('short_answers') else ''
record_ttls[type_][domain] = record['ttl']
for record_type, records in record_types.items():
write_records(f, record_type, records, record_ttls)
record_counts = {record_type: len(records) for record_type, records in record_types.items()}
for record_type, count in record_counts.items():
if count != 0:
print(f"Found {count} records of type {record_type}")
if __name__ == "__main__":
main()