Last active 1531162129

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

Revision ba7ec80f6cb1b53290e6e4530b9676d4cfdd7bd4

lyrics2ptr.py Raw
1#!/usr/bin/env python
2__usage__ = "{} $network <lyrics"
3__example__ = """
4$ {} 172.16.1.0/30 <<EOF
5this is a test
6this is only a test
7EOF
8"""
9
10import sys, ipaddress
11from string import ascii_lowercase,ascii_uppercase
12
13if 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
18subnet = iter(ipaddress.ip_network(sys.argv[1]))
19
20
21def filter(l):
22 return "".join([c for c in l if c in ascii_lowercase+ascii_uppercase+"_"])
23
24
25def 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
40for line in sys.stdin:
41 get_ip_for_line(line)