IP Octet Patern Match

jason petrone jp at NOSPAMdemonseed.net
Mon Jan 22 19:20:17 EST 2001


Kevin Fiscus <kfiscus at tampabay.rr.com> wrote:

> I am extremely new to Python.  Can anyone help me to design a script
> that will find the IP addresses from email headers and write the entire
> line where they are found to another text file?

I do something similar to this to process my ipfilter logs.

-------------------------

import sys, re
lines = sys.stdin.readlines()
for line in lines:
  if re.search('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', line): 
    print line[:len(line)-1]

-------------------------

I'm using 'print line[:len(line)-1]' instead of 'print line' in order to strip
off the trailing new line.  You might need -2 if it is dos formatted text.

Notice this doesn't handle IPV6 addresses.

-jason



More information about the Python-list mailing list