Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions apache_log_parsing/apache_log_parsing_v3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import re
import csv
import argparse
from collections import Counter

IP_REGEX = r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"

# Create the parser
parser = argparse.ArgumentParser(description='Reading the log and CSV file')
parser.add_argument("--l", "--logfile",
help='Please enter the logfile to parse',
dest="logfile",
type=argparse.FileType('r'),
required=True)

def extract_ips(logfile):
"""Extracts all IP addresses from the given logfile."""
try:
with open(logfile, 'r') as f:
log = f.read()
except Exception as e:
print(f"Error reading file: {e}")
return []
return re.findall(IP_REGEX, log)

def count_ips(ip_list):
"""Count the occurrence of each IP address in the list."""
return Counter(ip_list)

def write_csv(counter):
"""Write the counter data to a CSV file."""
try:
with open("ip_count.csv", 'w') as f:
writer = csv.DictWriter(f, fieldnames=["IP_Address", "Count"])
writer.writeheader()
for item, count in counter.items():
writer.writerow({"IP_Address": item, "Count": count})
except Exception as e:
print(f"Error writing CSV file: {e}")

def main():
args = parser.parse_args()
ip_list = extract_ips(args.logfile)
ip_counter = count_ips(ip_list)
write_csv(ip_counter)

if __name__ == '__main__':
main()