Last active 1531162129

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

lyrics2ptr.py Raw
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
6__usage__ = "{} $network <lyrics"
7__example__ = """
8$ {} 172.16.1.0/30 <<EOF
9this is a test
10this is only a test
11EOF
12"""
13
14import sys, ipaddress
15from string import ascii_lowercase,ascii_uppercase
16
17if len(sys.argv) < 2 or sys.stdin.isatty():
18 print(__usage__.format(sys.argv[0]))
19 print(__example__.format(sys.argv[0]))
20 sys.exit(2)
21
22subnet = iter(ipaddress.ip_network(sys.argv[1]))
23
24
25def filter(l):
26 return "".join([c for c in l if c in ascii_lowercase+ascii_uppercase+"_"])
27
28
29def get_ip_for_line(line):
30 l = []
31 overflow = []
32 for word in line.strip().split():
33 word = filter(word.lower())
34 if not l or (len(".".join(l)) + len(word) < 253) and not overflow:
35 if len(word) > 60:
36 while word:
37 l.append(word[:60])
38 word = word[60:]
39 else:
40 l.append(word)
41 else:
42 overflow.append(word)
43 ip = ".".join(str(next(subnet)).split(".")[::-1])
44 print("^{}.in-addr.arpa:{}:86400".format(ip, ".".join(l)))
45 if overflow:
46 get_ip_for_line(" ".join(overflow))
47
48
49for line in sys.stdin:
50 try:
51 get_ip_for_line(line)
52 except StopIteration:
53 print("out of IPs!", file=sys.stderr)
54 break