Python Forum
I am completely lost on this homework assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am completely lost on this homework assignment
#1
Step 1 (50 pts):

The objective of this program is simply to open a file, read each line and display it. Your program should:

Open auth.log.1b. (See note below).
Read each line.
Display each line.
Note that your program could take over 15 seconds to complete.
Save this program as step_1.py.

This is what I have for step 1 and it worked.
text_file = open("auth.log.1b")
contents = text_file.read()
text_file.close()
print(contents)
Step 2
The objective of this step is to recognize which lines indicate an attack. Start this step by making a copy of your Step 1 program.

Your program should do the following:

As each line is read, check to see if “Failed password” is in the line by using the ‘in’ operator.
If “Failed password” is in the line, display the line.
Otherwise do not.
Save this program as step_2.py.
For step 2 I am lost on what to do if anyone could help me out that would be great.

Step 3
This step is perhaps the most challenging. The objective here is to slice the user name out of the lines that include “Failed password”. Begin by making a copy of step_2.py.

Strategy:

You will slice the user name, which varies in length and start position within the line, by finding a pattern that always appears immediately before it and another pattern that always appears immediately after it. Use these offsets to compute the starting and ending values of the user name slice.

Your program should do the following:

For just the lines that include “Failed password”:
Use the string find() method to get the offset of “invalid user ” from the start of the line.
Determine the starting point of the user name slice by adding the length of “invalid user “ to the offset provided by find(). This will be the index of the first character in the user name.
Use the string find() method to get the offset of “ from “. This will serve as the end point of the user name slice.
Slice the user name from the line using the starting and ending points and store the result in a variable.
Instead of displaying the whole line, just display the slice. You should see ‘root’ displayed quite a bit.

Step 4
The objective of this step is to use a dictionary to count the number of times each user name appears in attack attempts. Begin by making a copy of step_3.py.

Strategy:

Up until this point, you've been working with an abbreviated log file, auth.log.1b, which only has data for attacks from user root. For this step, you'll need the full log file: auth.log.1. Download auth.log.1. Download it just as you did with auth.log.1b and change your code to open auth.log.1 instead of auth.log.1b. Note that your program could take considerably longer to run using auth.log.1.

Use a dictionary to count the number of times each user name appears in the file. The items in the dictionary will consist of a user name as the key and a count as the value.

Your program should do the following:

Create an empty dictionary at the top of the program right after the header comments.
After you have sliced a name, use the in operator to see if it is already in the dictionary
If it is not in the dictionary, add it. Do so by using the user name as the key and set the value to 1.
If it is in the dictionary, use the user name as a key to get the current value. Add 1 to the current value and store it back in the dictionary.
After all of the lines of the file have been read, use a for loop to display each key and value. The key will be a user name and the value will be the number of times the user name appears in the dictionary.

The last 2 steps I have not began working on yet but help on those would be greatly appreciated also!
buran write Feb-21-2022, 08:11 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Read carefully your assignment and write the code as per requirements. The assignment gives you very detailed instructions how to complete each step.

For step 1 note
(Feb-21-2022, 04:29 AM)a36 Wrote: Read each line.
Reading line by line 8s essential for completing next steps. Don't read the file as a whole.
Larz60+ likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  saving issue on homework assignment russoj5 2 1,971 Oct-26-2020, 01:53 PM
Last Post: russoj5
  Very Lost vollynez 3 1,850 Oct-02-2020, 09:09 PM
Last Post: Yoriz
  Help with homework assignment grayarea3 1 2,867 Feb-20-2019, 10:10 PM
Last Post: ichabod801
  I got lost in the program Gadev 1 1,875 Nov-11-2018, 05:50 PM
Last Post: j.crater
  I keep getting errors and I'm lost! samjonsnell 9 4,876 Oct-28-2018, 11:38 PM
Last Post: samjonsnell
  Homework Assignment Help pinku018 3 3,071 Jun-08-2018, 01:09 PM
Last Post: j.crater
  Homework Assignment Help sphedicl 3 3,285 Jun-08-2018, 12:26 PM
Last Post: pinku018
  test..and I'm already lost sannixinc 1 2,495 Feb-22-2018, 09:09 PM
Last Post: glidecode
  Completly lost about to fail my class! crakacheezy 7 4,525 Dec-28-2017, 11:50 PM
Last Post: mepyyeti

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020