Skip to content

Instantly share code, notes, and snippets.

@alexwoolford
Created October 25, 2014 19:03
Show Gist options
  • Save alexwoolford/ed24f5fb621ce16670cb to your computer and use it in GitHub Desktop.
Save alexwoolford/ed24f5fb621ce16670cb to your computer and use it in GitHub Desktop.
How to parse valid IP's from a log file (Stackoverflow answer)
"""
http://stackoverflow.com/questions/26564513/python-valid-ips-from-each-line-on-a-text-file/26564920#26564920
iplog.txt:
Host : 75.75.75.75 , DNS : resolved dns , Location : USA
Host : 266.266.266.266 , DNS : resolved dns , Location : USA
Host : 10.0.1.1 , DNS : resolved dns , Location : USA
ipclear.txt:
75.75.75.75
10.0.1.1
"""
import re
import socket
ipPattern = re.compile('Host : (.*) , DNS : .*')
outfile = open('ipclear.txt', 'w')
for line in open('iplog.txt').readlines():
ipString = ipPattern.match(line).group(1)
try:
socket.inet_aton(ipString)
outfile.write(ipString + '\n')
except:
pass
outfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment