Python Forum
hardest week yet: some guidence please
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
hardest week yet: some guidence please
#1
Here is the order of my assignment

Step 1
The objective of this program is simply to open a file, read each line and display it. Your program should:
Open auth.log.1.
Read each line.
Display each line.
Save this program as step_1.py.
with open("auth.log.1") as auth_log:
    for line in auth_log:
        line = line.strip()
        print(line)
Problem with this is that it does not print the whole file in pycharm but it does in idle.


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.
with open("auth.log.1") as auth_log:
    for line in auth_log:
        if "Failed password" in auth_log:
            line = line.strip()
            print(line)
Problem with step 2 it does not print anything and I get no errors

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.
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.

Save your program as step_3.py.
Step 3 and 4 I dont know were to begin
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.
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.

Save your file as hwk6.py
Reply


Messages In This Thread
hardest week yet: some guidence please - by raymond2688 - Aug-16-2019, 05:40 PM
slice help - by raymond2688 - Aug-17-2019, 12:52 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Calculating the number of day of the week RbaPhoenix 3 2,541 Mar-27-2020, 09:23 PM
Last Post: Larz60+
  week 1 python help - string formatting ? raymond2688 20 8,003 Jul-09-2019, 08:10 PM
Last Post: snippsat
  Trying to get the week number from a string of numbers fad3r 2 3,253 Apr-15-2018, 06:52 PM
Last Post: ljmetzger

Forum Jump:

User Panel Messages

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