-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport-bind.py
69 lines (55 loc) · 1.78 KB
/
export-bind.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
66
67
68
69
import re
import sys
import xml.etree.ElementTree as ET
import requests
try:
api_key = sys.argv[1]
domain_name = sys.argv[2]
except:
print("Invalid arguments. Usage: export-bind.py ZONOMI-API-KEY domain.name")
exit(13)
# noinspection PyUnboundLocalVariable
f = open(domain_name + ".zone.txt", "w")
r = requests.get("https://zonomi.com/app/dns/dyndns.jsp?action=QUERY&name=**.%s&api_key=%s" % (domain_name, api_key))
root = ET.fromstring(r.content)
actions = root.find("actions")
action = actions.find("action")
def isIpAddress(target):
if re.match(r'^((\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])$', target):
return True
else:
return False
def get_content_fqn(target):
if isIpAddress(target):
return target
else:
return target + "."
for record in action.iter("record"):
source_name = record.attrib.get("name")
type = record.attrib.get("type")
source_content = record.attrib.get("content")
if source_name == domain_name:
name = " "
elif "." + domain_name in source_name:
name = source_name.replace("." + domain_name, "")
else:
name = source_name + "."
if type == "NS" or type == "SOA": continue
if type == "CNAME":
content = get_content_fqn(source_content)
elif type == "MX":
content = "%s\t%s" % (record.attrib.get("prio"), get_content_fqn(source_content))
elif type == "TXT":
content = "\"%s\"" % source_content
else:
content = get_content_fqn(source_content)
f.write(
"%s\t%s\t%s\t%s\t%s\n" % (
name,
record.attrib.get("ttl").replace(" seconds", ""),
"IN",
type,
content
)
)
print("Exporting record: %s " % record.attrib)