简体   繁体   English

拆分IP地址的最后一个八位字节时遇到麻烦

[英]trouble splitting off last octet of ip address

I am having trouble splitting the last octet off my IP address and printing the result to a file. 我在将最后一个八位位组从我的IP地址中分离并将结果打印到文件时遇到麻烦。

This is the current output 这是当前输出

IN    PTR   10.102.36.38.
IN    PTR   .
IN    PTR   192.168.100.11.
IN    PTR   192.168.100.12.

this is the desired output to be written to a file. 这是要写入文件的所需输出。

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)
import sys
import re

if len(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: ")

with open(yamlfile) as infile:
    with open("/tmp/blocked_ips_test", "a") as outfile:
        ips = set()
        for text in infile:
            text = text.rstrip()
            matched = re.search(r'(\d{,3}\.){3}\d{,3}', text)
            if matched:
                ips.add(matched.group())

        for ip in ips:
            line = '%s    IN    PTR   %s.' % (ip.rsplit('.', 1)[-1], ip)
            print line
            print >>outfile, line

Something like that would do the trick (Python 2 & 3): 这样的事情就可以解决问题(Python 2和3):

import re

r = re.compile(r'^IN\s+PTR\s+([\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.([\d]{1,3})\.)\s*$')

with open("my.zone", "rt") as file:
    for line in file:
        m = r.match(line)
        if m:
            print("{1} IN PTR {0}".format(*m.groups()))

Having your source data in "my.zone": 将您的源数据放在“ my.zone”中:

sh$ cat my.zone
IN    PTR   10.102.36.38.
IN    PTR   .
IN    PTR   192.168.100.11.
IN    PTR   192.168.100.12.

This produce the following result (with non-matching lines silently removed): 产生以下结果(不匹配的行被静默删除):

38 IN PTR 10.102.36.38.
11 IN PTR 192.168.100.11.
12 IN PTR 192.168.100.12.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM