Last active
September 22, 2024 00:31
-
-
Save gwire/0440646815d002af0aaf1dd996c567b4 to your computer and use it in GitHub Desktop.
Generate IPv6 AAAA records in tinydns format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# tinyipv6 - generate an IPv6 type 28 AAAA record in the tinydns format | |
# | |
# example: ./tinyipv6.py -l 60 -d host.example.com -i 2001:db8:85a3:8d3:1319:8a2e:370:7348 | |
# :host.example.com:28:\040\001\015\270\205\243\010\323\023\031\212\056\003\160\163\110:60 | |
# | |
# -3 and -6 options will generate "3:" and "6:" type records, if your server supports them | |
# | |
# 2022 Lee Maguire | |
import ipaddress | |
import sys | |
import getopt | |
ttl = "86400" | |
domain= "host.example.com" | |
ip = "2001:db8:85a3:8d3:1319:8a2e:370:7348" | |
opt_r = 0 | |
opt_3 = 0 | |
opt_6 = 0 | |
def octIPv6( ipv6addr ): | |
ip6_bytes = ipaddress.IPv6Address(ipv6addr).packed | |
output = "" | |
for j in range(16): | |
output += "\\{0:03o}".format(ip6_bytes[j]) | |
return( output ) | |
def escapeText( text ): | |
output = "" | |
for c in text: | |
if c in ["\r","\n","\t",":"," ","\\","/"]: | |
output = output + "\\{0:03o}".format(ord(c)) | |
else: | |
output = output + c | |
return( output ); | |
def tinyAAAARecord( fqdn, ipv6addr, ttl ): | |
return( ":" + escapeText( fqdn ) + ":28:" + octIPv6(ipv6addr) + ":" + ttl ); | |
def tiny3Record( fqdn, ipv6addr, ttl ): | |
return( "3" + escapeText( fqdn ) + ":" + str(ipaddress.IPv6Address(ipv6addr).exploded).replace(":","") + ":" + ttl ); | |
def tiny6Record( fqdn, ipv6addr, ttl ): | |
return( "6" + escapeText( fqdn ) + ":" + str(ipaddress.IPv6Address(ipv6addr).exploded).replace(":","") + ":" + ttl ); | |
opts, args = getopt.getopt(sys.argv[1:],"hd:i:l:36r",["domain=","ip=","ttl=","raw"]) | |
for opt, arg in opts: | |
if opt == '-h': | |
print('Usage: tinyipv6.py -d host.example.com -i 2001:db8:85a3:8d3:1319:8a2e:370:7348 -l 60 [-r][-3][-6]') | |
sys.exit() | |
elif opt in ("-d", "--domain"): | |
domain = arg | |
elif opt in ("-i", "--ip"): | |
ip = arg | |
elif opt in ("-l", "--ttl"): | |
ttl = arg | |
elif opt in ("-r", "--raw"): | |
opt_r = 1 | |
elif opt in ("-3"): | |
opt_3 = 1 | |
elif opt in ("-6"): | |
opt_6 = 1 | |
## default to the raw format if not specified | |
if not opt_3 and not opt_6: | |
opt_r = 1 | |
if opt_r: | |
line = tinyAAAARecord( domain, ip, ttl) | |
sys.stdout.write( line + "\n") | |
if opt_3: | |
line = tiny3Record( domain, ip, ttl) | |
sys.stdout.write( line + "\n") | |
if opt_6: | |
line = tiny3Record( domain, ip, ttl) | |
sys.stdout.write( line + "\n") | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment