forked from WiiLink24/DNS-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdns-convert.py
More file actions
60 lines (49 loc) · 1.87 KB
/
dns-convert.py
File metadata and controls
60 lines (49 loc) · 1.87 KB
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
import json
def parse_dnsmasq_config_json(file_path):
entries = []
with open(file_path, 'r') as f:
for line in f:
line = line.strip()
if line.startswith('address=') and '/' in line:
try:
_, rest = line.split('=', 1)
parts = rest.split('/')
if len(parts) == 3:
_, domain, ip = parts
entry = {
"type": "a",
"name": domain,
"value": ip
}
entries.append(entry)
except ValueError:
continue # Skip malformed lines
return entries
def write_json(entries, output_path):
with open(output_path, 'w') as f:
json.dump(entries, f, indent=3)
def parse_dnsmasq_config_txt(file_path):
entries = ""
with open(file_path, 'r') as f:
for line in f:
line = line.strip()
if line.startswith('address=') and '/' in line:
try:
_, rest = line.split('=', 1)
parts = rest.split('/')
if len(parts) == 3:
_, domain, ip = parts
entry = f"{ip} {domain}\n"
entries = entries + entry
except ValueError:
continue # Skip malformed lines
return entries
# Example usage
if __name__ == "__main__":
config_path = 'dnsmasq.conf' # Input file path
output_json_path = 'dns_zones.json' # Output JSON path
output_txt_path = 'dns_zones-hosts.txt'
json_entries = parse_dnsmasq_config_json(config_path)
write_json(json_entries, output_json_path)
txt_entries = parse_dnsmasq_config_txt(config_path)
open(output_txt_path, 'w').write(txt_entries)