我无法从我的 IP 地址中拆分最后一个八位字节并将结果打印到文件中。
这是当前输出
IN    PTR   10.102.36.38.
IN    PTR   .
IN    PTR   192.168.100.11.
IN    PTR   192.168.100.12.
这是要写入文件的所需输出。
38   IN    PTR   10.102.36.38.
( deleted  this line ) IN    PTR   .
11   IN    PTR   192.168.100.11.
12   IN    PTR   192.168.100.12.
#!/usr/bin/env python
import sys
import re
from subprocess import call
try:
    if sys.argv[1:]:
        print "File: %s" % (sys.argv[1])
        yamlfile = sys.argv[1]
    else:
        yamlfile = raw_input("Please enter the yaml server file to  parse, e.g /etc/puppet/hieradata/*.yaml: ")
    try:
        file = open(yamlfile, "r")
        ips = []
        for text in file.readlines():
           text = text.rstrip()
           regex = re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})$',text)
           if regex is not None and regex not in ips:
               ips.append(regex)
        for ip in ips:
           outfile = open("/tmp/blocked_ips_test", "a")
#           addy = "".join(ip)
           #octet = call("cut", "-d.", "-f4")
           addy = 'IN    PTR'   '.'.join(reversed(ip)) + "."
           #addy = '.'.join(reversed(ip)) + "."
           #addy = '.'.join(reversed(ip)) + ".in-addr.arpa."
           if addy is not '':
              print  "IN    PTR   %s " % (addy)
              #print "IN    PTR   %s " % (addy) >>outfile
              outfile.write(addy)
              outfile.write("\n")
    finally:
        file.close()
        outfile.close()
except IOError, (errno, strerror):
        print "I/O Error(%s) : %s" % (errno, strerror)