-
Notifications
You must be signed in to change notification settings - Fork 19
/
axfr2route53
executable file
·67 lines (58 loc) · 1.93 KB
/
axfr2route53
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
66
67
#!/usr/bin/env python3
# Author: Michael Ludvig - https://aws.nz/aws-utils/axfr2route53
# License: BSD
# Convert DNS zone from Bind's "dig axfr" format to AWS Route53 JSON input
# Usage:
# 1) dig axfr example.com. @ns.example.com. | axfr2route53 > example-com.json
# 2) aws route53 change-resource-record-sets --hosted-zone-id ZABCDEF123456 --change-batch file://example-com.json
# 3) Update list of nameservers to AWS ones through example.com's registrar
import re
import sys
import json
if len(sys.argv) > 1:
inp = open(sys.argv[1], 'r')
else:
inp = sys.stdin
changes = {}
zone_name = ""
while True:
line = inp.readline()
if not line:
break
line = line.strip()
rr = re.match('^(?P<name>\S+)\s+(?P<ttl>\d+)\s+(?P<class>\w+)\s+(?P<type>\w+)\s+(?P<data>.*)$', line)
if not rr:
continue
if rr.group('class') != "IN":
sys.stderr.write("Unknown class, only supporting IN: %s" % line)
continue
if rr.group('type') == "SOA":
# Ignore SOA records
zone_name = rr.group('name')
continue
if rr.group('type') == "NS" and rr.group('name') == zone_name:
# Ignore zone NS records
continue
# Merge records of the same type, e.g. all A records for the same name
change_id = "%(name)s/%(type)s" % rr.groupdict()
if change_id in changes:
changes[change_id]["ResourceRecordSet"]["ResourceRecords"].append({
"Value": rr.group('data')
})
else:
changes[change_id] = {
"Action" : "CREATE",
"ResourceRecordSet" : {
"Name" : rr.group('name'),
"Type" : rr.group('type'),
"TTL" : int(rr.group('ttl')),
"ResourceRecords" : [{
"Value" : rr.group('data')
}]
}
}
if changes:
change_batch = {
"Changes": list(changes.values())
}
print(json.dumps(change_batch, indent=2))