Skip to content

Instantly share code, notes, and snippets.

@lsgalves
Created August 14, 2023 03:30
Show Gist options
  • Save lsgalves/6d7427c54250c3d6691361ca4796f490 to your computer and use it in GitHub Desktop.
Save lsgalves/6d7427c54250c3d6691361ca4796f490 to your computer and use it in GitHub Desktop.
Convert TinyDNS zone file to Bind format.
#!/usr/bin/env python3
import sys
records = []
if len(sys.argv) < 2:
print('Usage: tinytobind.py ZONEFILE')
sys.exit(0)
with open(sys.argv[1], 'r') as zonefile:
for line in zonefile:
line = line.strip()
if len(line):
line = line.replace('\\052', '*')
line = line.replace('\\057', '/')
parts = line[1:].split(':')
parts = list(map(lambda p: p.replace('\\072', ':'), parts))
hostname = parts[0]
# Comment
if line[0] == '#':
print(line[1:].strip())
elif line[0] == '+':
value = parts[1]
ttl = parts[2]
print(f'{hostname} {ttl} IN A {value}')
elif line[0] == 'C':
value = parts[1]
ttl = parts[2]
print(f'{hostname} {ttl} IN CNAME {value}')
elif line[0] == "'":
value = parts[1]
ttl = parts[2]
print(f'{hostname} {ttl} IN TXT "{value}"')
elif line[0] == '.':
value = parts[2]
ttl = parts[3]
print(f'{hostname} {ttl} IN NS {value}')
elif line[0] == '@':
if parts[1]:
pass
else:
value = parts[2]
priority = parts[3]
ttl = parts[4]
print(f'{hostname} {ttl} IN MX {priority or 10} {value}')
elif line[0] == ':':
rtype = 'SRV'
value = parts[2]
ttl = parts[3]
if parts[1] == '33':
value
print(f'{hostname} {ttl} IN {rtype} {value}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment