Last active 1531162129

Takes lyrics and a subnet, returns djbdns PTR records for those lyrics

Steven Smith revised this gist 1531198129. Go to revision

1 file changed, 15 insertions, 2 deletions

lyrics2ptr.py

@@ -1,4 +1,8 @@
1 1 #!/usr/bin/env python
2 + # v4
3 + # known bugs:
4 + # * very long words may break all the things or end up longer than the maximum limit
5 +
2 6 __usage__ = "{} $network <lyrics"
3 7 __example__ = """
4 8 $ {} 172.16.1.0/30 <<EOF
@@ -24,18 +28,27 @@ def filter(l):
24 28
25 29 def get_ip_for_line(line):
26 30 l = []
31 + overflow = []
27 32 for word in line.strip().split():
28 33 word = filter(word.lower())
29 - if len(".".join(l)) + len(word) < 253:
34 + if not l or (len(".".join(l)) + len(word) < 253) and not overflow:
30 35 if len(word) > 60:
31 36 while word:
32 37 l.append(word[:60])
33 38 word = word[60:]
34 39 else:
35 40 l.append(word)
41 + else:
42 + overflow.append(word)
36 43 ip = ".".join(str(next(subnet)).split(".")[::-1])
37 44 print("^{}.in-addr.arpa:{}:86400".format(ip, ".".join(l)))
45 + if overflow:
46 + get_ip_for_line(" ".join(overflow))
38 47
39 48
40 49 for line in sys.stdin:
41 - get_ip_for_line(line)
50 + try:
51 + get_ip_for_line(line)
52 + except StopIteration:
53 + print("out of IPs!", file=sys.stderr)
54 + break

Steven Smith revised this gist 1531197433. Go to revision

1 file changed, 41 insertions

lyrics2ptr.py(file created)

@@ -0,0 +1,41 @@
1 + #!/usr/bin/env python
2 + __usage__ = "{} $network <lyrics"
3 + __example__ = """
4 + $ {} 172.16.1.0/30 <<EOF
5 + this is a test
6 + this is only a test
7 + EOF
8 + """
9 +
10 + import sys, ipaddress
11 + from string import ascii_lowercase,ascii_uppercase
12 +
13 + if len(sys.argv) < 2 or sys.stdin.isatty():
14 + print(__usage__.format(sys.argv[0]))
15 + print(__example__.format(sys.argv[0]))
16 + sys.exit(2)
17 +
18 + subnet = iter(ipaddress.ip_network(sys.argv[1]))
19 +
20 +
21 + def filter(l):
22 + return "".join([c for c in l if c in ascii_lowercase+ascii_uppercase+"_"])
23 +
24 +
25 + def get_ip_for_line(line):
26 + l = []
27 + for word in line.strip().split():
28 + word = filter(word.lower())
29 + if len(".".join(l)) + len(word) < 253:
30 + if len(word) > 60:
31 + while word:
32 + l.append(word[:60])
33 + word = word[60:]
34 + else:
35 + l.append(word)
36 + ip = ".".join(str(next(subnet)).split(".")[::-1])
37 + print("^{}.in-addr.arpa:{}:86400".format(ip, ".".join(l)))
38 +
39 +
40 + for line in sys.stdin:
41 + get_ip_for_line(line)
Newer Older