lyrics2ptr.py
· 1020 B · Python
Raw
#!/usr/bin/env python
__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 = []
for word in line.strip().split():
word = filter(word.lower())
if len(".".join(l)) + len(word) < 253:
if len(word) > 60:
while word:
l.append(word[:60])
word = word[60:]
else:
l.append(word)
ip = ".".join(str(next(subnet)).split(".")[::-1])
print("^{}.in-addr.arpa:{}:86400".format(ip, ".".join(l)))
for line in sys.stdin:
get_ip_for_line(line)
| 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) |