3 from netaddr import IPAddress, AddrFormatError
6 print >>sys.stderr, "Usage: %s filename" % sys.argv[0]
9 log = open(sys.argv[1], "a")
11 # group 1 must be the prefix, group 2 the IP, group 3 the suffix
12 ipmatch = r"([0-9a-f.:]+)"
13 accesslog = re.compile(r"^(\S+ )"+ipmatch+r"( .*)$")
14 errorlog = re.compile(r"^(.* \[client )"+ipmatch+r"(:[0-9]+\] .*)$")
17 line = sys.stdin.readline()
20 match = accesslog.search(line)
22 match = errorlog.search(line)
28 prefix = match.group(1)
30 suffix = match.group(3)
32 ip = IPAddress(ip) # parse the addres
33 ip = ip & (IPAddress('255.255.255.0') if ip.version == 4 else IPAddress('ffff:ffff:ffff::')) # mask out a bunch of bits
34 # now we have a parsed representation of the IP address we want to print
35 log.write(prefix+str(ip)+suffix+"\n")
36 except (ValueError, AddrFormatError):
37 # not actually an IP address...