-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_log_ip.py
31 lines (23 loc) · 1016 Bytes
/
parse_log_ip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/python
###############################################################################################
## This file will extract the ip address from a file and return it as a dictionary data structure
## with the line number as the key and the value as a nested dictionary with 'ip' as the key and
## the ip address as the value
################################################################################################
import re
class Parser:
ip_regex = r'\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b'
output = {}
def parse(self, filename):
self.ip_regex=(r'\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b')
with open(filename) as infile:
for line_number, line in enumerate(infile):
result = re.search(self.ip_regex, line)
if result:
groups = result.group()
self.output[line_number] = {}
self.output[line_number]['ip'] = groups
print self.output
if __name__ == "__main__":
p = Parser()
p.parse('logfile name')