Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given sample log data from a firewall, create a program that prepares the data for analysis. Make a LogEntry Class: Make a new file named

Given sample log data from a firewall, create a program that prepares the data for analysis.
Make a LogEntry Class:
Make a new file named log_analyzer.py in the directory you created for this application. This file will be a module that you will include into your main directive code.
In the log_analyzer.py file, make a new Python class named LogEntry. Each object of class LogEntry will represent one line in the CSV file.
The LogEntry class will have the following attributes: event_time, internal_ip, port_number, protocol, action, rule_id, source_ip, country, and country_name. The other fields are optional.
The CSV file contains a string with the date and time information. In the initializer for the LogEntry class, use the Python datetime package to ensure that the value for event_date is stored as a datetime object. (This will be important later.)
Add a property to the LogEntry class called ipv4_class that uses a regular expression to examine the source_ip and returns the string value of the IPv4 address class (which will be either "A","B","C", or "D") based on the public IP address range. For example, the source_ip 11.177.69.220 will return "A" and the source_ip 173.205.219.112 will return "C".
Think: How can you find just the first component of an IPv4 address?
Write Unit Tests for the LogEntry Class:
make a new file named test_log_analyzer.py in the directory you created for this application. Write several unit tests that evaluate the following functionality of your LogEntry class:
1. Ensure that the event_time string is properly converted to a datetime object.
a. For example, you could make a new instance of LogEntry with a string value of "2022-01-0108:29:25 UTC" for the event_time. You could then assert that the month of the resulting datetime object ==1.
b. You could also assert that the hour of the resulting datetime object ==8.
2. Ensure that the ipv4_class returns the correct IPv4 address class
a. For example, you can assert that the ipv4_class =="D" for the source_ip "229.163.4.51"
Write several tests (asserts) for each of the above scenarios to ensure your class is behaving correctly. Make sure all your tests pass before moving on.
THINK: What else could go wrong when converting the CSV to objects of class LogEntry?
Import the Firewall Log Data:
Make an index.py file in the root directory for this program (the same directory that contains your CSV file and the log_analyzer.py file).
Import the LogEntry class from the log_analyzer.py file (from log_analyzer import LogEntry), the argparse module, and the csv module.
Then make a main() method and add the if __name__... syntax at the end of the file so that this program can be executed from the command line.
make a method to define and parse the command line arguments for your program. Your program will require ONE argument: --filename (the filename of your CSV file)
In the main() method, do the following:
1. Obtain the filename of the CSV file with your log data and store it in a variable.
2. Use the csv.DictReader class to read the data from the firewall log CSV. Remember that this will import a "list of dictionaries".
3. For each element of the list, make an instance of the LogEntry class and provide the values for each parameter expected by the class initializer.
4. Add each LogEntry object to a list (you will now have a list of objects of class LogEntry).
5. Loop through the FIRST FIVE ELEMENTS of the list of LogEntry objects and print the following to the screen for each:
a. Date of the log entry formatted as a US- style string (Month/Day/Year Hour:Minute TimeZone) in Eastern Time
b. Action
c. Source_ip
d. IPv4 Class
e. Country Name (the country where the connection came from)
attached below is an image of the CSV file
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 10g SQL

Authors: Joan Casteel, Lannes Morris Murphy

1st Edition

141883629X, 9781418836290

More Books