lyrics2ptr.py
· 1.3 KiB · Python
Raw
#!/usr/bin/env python
# v4
# known bugs:
# * very long words may break all the things or end up longer than the maximum limit
__usage__ = "{} $network <lyrics"
__example__ = """
$ {} 172.16.1.0/30 <<EOF
this is a test
this is only a test
EOF
"""
import sys, ipaddress
from string import ascii_lowercase,ascii_uppercase
if len(sys.argv) < 2 or sys.stdin.isatty():
print(__usage__.format(sys.argv[0]))
print(__example__.format(sys.argv[0]))
sys.exit(2)
subnet = iter(ipaddress.ip_network(sys.argv[1]))
def filter(l):
return "".join([c for c in l if c in ascii_lowercase+ascii_uppercase+"_"])
def get_ip_for_line(line):
l = []
overflow = []
for word in line.strip().split():
word = filter(word.lower())
if not l or (len(".".join(l)) + len(word) < 253) and not overflow:
if len(word) > 60:
while word:
l.append(word[:60])
word = word[60:]
else:
l.append(word)
else:
overflow.append(word)
ip = ".".join(str(next(subnet)).split(".")[::-1])
print("^{}.in-addr.arpa:{}:86400".format(ip, ".".join(l)))
if overflow:
get_ip_for_line(" ".join(overflow))
for line in sys.stdin:
try:
get_ip_for_line(line)
except StopIteration:
print("out of IPs!", file=sys.stderr)
break
| 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 |
| 9 | this is a test |
| 10 | this is only a test |
| 11 | EOF |
| 12 | """ |
| 13 | |
| 14 | import sys, ipaddress |
| 15 | from string import ascii_lowercase,ascii_uppercase |
| 16 | |
| 17 | if 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 | |
| 22 | subnet = iter(ipaddress.ip_network(sys.argv[1])) |
| 23 | |
| 24 | |
| 25 | def filter(l): |
| 26 | return "".join([c for c in l if c in ascii_lowercase+ascii_uppercase+"_"]) |
| 27 | |
| 28 | |
| 29 | def 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 | |
| 49 | for 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 |