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
#2
For step 2, you should be checking in line, not in auth_log. Once you have that fixed, do you see a pattern in the output from step 2? If so, you use that pattern in step 3.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Step 2
if "Failed password" in auth_log:
needs to check in line not auth_log
Reply
#4
(Aug-16-2019, 05:40 PM)raymond2688 Wrote: Problem with this is that it does not print the whole file in pycharm but it does in idle.

What does it do from the terminal? That's the only place that matters (unless you have different instructions for the course).
Reply
#5
(Aug-16-2019, 06:16 PM)ichabod801 Wrote: For step 2, you should be checking in line, not in auth_log. Once you have that fixed, do you see a pattern in the output from step 2? If so, you use that pattern in step 3.

thank you I just figured that out prior to checking back in I am now working on step 3, with 1 and 2 functional.

(Aug-16-2019, 06:19 PM)nilamo Wrote: What does it do from the terminal? That's the only place that matters (unless you have different instructions for the course).
It works fine in terminal. I now have step 1 and 2 complete thank you

My step 3
The word before every user name is user and the word after is from. I wrote this code which i think is the right format (ofcorse it doesnt work)
intruder_log = {}

with open("auth.log.1") as auth_log:
    for line in auth_log:
        if "Failed password" in line:
            start = intruder_log.find("user")
            end = intruder_log.find("from")

            invalid_user = intruder_log[start : end ]

            print("invalid_user: {}".format(invalid_user))
Reply
#6
In lines 6 and 7, it appears that intruder_log is still {} so you won't find what you are looking for.
Reply
#7
(Aug-16-2019, 07:30 PM)jefsummers Wrote: In lines 6 and 7, it appears that intruder_log is still {} so you won't find what you are looking for.

so im thinking that i need to replace {} with []

(Aug-16-2019, 08:07 PM)raymond2688 Wrote:
(Aug-16-2019, 07:30 PM)jefsummers Wrote: In lines 6 and 7, it appears that intruder_log is still {} so you won't find what you are looking for.

so im thinking that i need to replace {} with []

correction () with []
Reply
#8
meaning intruder_log is assigned as {}
then you try and use find on intruder_log which wont work as find is a string method.

you need to
Quote:Use the string find() method to get the offset of “invalid user ” from the start of the line.
Reply
#9
Besides being the wrong type, intruder_log is empty. You won't find anything.
Reply
#10
(Aug-16-2019, 09:19 PM)jefsummers Wrote: Besides being the wrong type, intruder_log is empty. You won't find anything.

yes it seems I got ahead of myself. I have removed the directory.
I am supposed to creat a slice
I have got this much done of step 3

text_file = open("auth.log.1")

with open("auth.log.1") as text_file:
    for line in text_file:
        if "Failed password" in line:
            start = line.find("user")
            end = line.find(" from")
            name = line[start:end]
            print("{}".format(name))
my output is the column of user names but it has user infront of it and I can not figure out why
my output

user root
user root
user root
user root
user root
user root
user root
user root
user root
user root
user root
user root
user root
user root
user root
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calculating the number of day of the week RbaPhoenix 3 2,501 Mar-27-2020, 09:23 PM
Last Post: Larz60+
  week 1 python help - string formatting ? raymond2688 20 7,824 Jul-09-2019, 08:10 PM
Last Post: snippsat
  Trying to get the week number from a string of numbers fad3r 2 3,203 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