Question

Write regular expressions that will match IP addresses and usernames in a Microsoft IIS log file....

Write regular expressions that will match IP addresses and usernames in a Microsoft IIS log file.

First, you will write a regular expression that matches IP addresses within the re.compile function call on line 36. Next, you will write a regular expression that matches usernames (in the format domain\username) on line 50. Finally, in steps 3 and 4 you will write a loop for each step that displays all the IP addresses (gathered into the list ipAddresses) and usernames (gathered in the list list users) to the console window.

# Import the regular expressions module

import re

# Function that takes a file and loads it into

# a Python list variable and returns that variable

def loadLogIntoList(filename):

   with open(filename) as f:

      lines = f.read().splitlines()

   return lines

# Ask the user to input a file name

filename = input("Type in the filename for the log to analyze: ")

# Load each line of the file into a list of strings

lines = loadLogIntoList(filename)

# Create a placeholder variable (a list of strings) to

# hold the IP addresses and users we find in the log file

ipAddresses = []

users = []

# Loop through each line of the file

for line in lines:

   ####  Part 1: Create a new python regular expression object

   ####          by compiling an expression to look for an

   ####          IP address

  

   ipRegEx = re.compile(r'REPLACE WITH A REGULAR EXPRESSION')

   

   # Search the contents of each line for a match. If there is a

   # match and that match doesn't already exist in the list, append

   # it to the list

   ipSearchResult = ipRegEx.search(line)

   if ipSearchResult != None and ipSearchResult.group() not in ipAddresses:

      ipAddresses.append(ipSearchResult.group())

      

   ####  Part 2: Create a new python regular expression object

   ####          by compiling an expression to look for a user

   ####          account(as a DOMAIN\username combination)

   userRegEx = re.compile(r'REPLACE WITH A REGULAR EXPRESSION')

   

   # Search the contents of each line for a match. If there is a

   # match and that match doesn't already exist in the list, append

   # it to the list

   userSearchResult = userRegEx.search(line)

   if userSearchResult != None and userSearchResult.group() not in users:

      users.append(userSearchResult.group())

####  Part 3: Loop through each IP address and display

####          each IP address found to the user

# WRITE CODE HERE

   

####  Part 4: Loop through each username (shown as a

####          DOMAIN\username combination) to the user

# WRITE CODE HERE

Homework Answers

Answer #1
def isPhoneNumber(text):
❶     if len(text) != 12:
           return False
       for i in range(0, 3):
❷         if not text[i].isdecimal():
               return False
❸     if text[3] != '-':
           return False
       for i in range(4, 7):
❹         if not text[i].isdecimal():
               return False
❺     if text[7] != '-':
           return False
       for i in range(8, 12):
❻         if not text[i].isdecimal():
               return False
❼     return True

   print('415-555-4242 is a phone number:')
   print(isPhoneNumber('415-555-4242'))
   print('Moshi moshi is a phone number:')
   print(isPhoneNumber('Moshi moshi'))
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In this assignment, you will analyze a log file from a web server to count the...
In this assignment, you will analyze a log file from a web server to count the number of hits made from each unique IP address. Step 1: Write the mapper, reducer, and driver code so that the final output of your program should be a file containing a list of IP addresses, and the number of hits from that address. The main idea is to examine the input data file to learn about the format of the input. Your mapper...
Python: Simple Banking Application Project Solution: • Input file: The program starts with reading in all...
Python: Simple Banking Application Project Solution: • Input file: The program starts with reading in all user information from a given input file. The input file contains information of a user in following order: username, first name, last name, password, account number and account balance. Information is separated with ‘|’. o username is a unique information, so no two users will have same username. Sample input file: Username eaglebank has password 123456, account number of BB12 and balance of $1000....
HIMT 345 Homework 06: Iteration Overview: Examine PyCharm’s “Introduction to Python” samples for Loops. Use PyCharm...
HIMT 345 Homework 06: Iteration Overview: Examine PyCharm’s “Introduction to Python” samples for Loops. Use PyCharm to work along with a worked exercise from Chapter 5. Use two different looping strategies for different purposes. Prior Task Completion: 1. Read Chapter 05. 2. View Chapter 05’s video notes. 3. As you view the video, work along with each code sample in PyCharm using the Ch 5 Iteration PPT Code Samples.zip. Important: Do not skip the process of following along with the...
(35 pts) Analyze the output from the tool Enum4Linux [See Appendix B]. List: Hostname & IP...
(35 pts) Analyze the output from the tool Enum4Linux [See Appendix B]. List: Hostname & IP address Domain/Workgroup Name All users found Is there any interesting info for any of the users? If so, how can this be used? Is Password Complexity enabled? What shares are available on this system? Starting enum4linux v0.8.9 ( http://labs.portcullis.co.uk/application/enum4linux/ ) on Fri Sep 25 17:51:22 2020 ========================== |    Target Information    | ========================== Target ........... 192.168.1.102 RID Range ........ 500-550,1000-1050 Username ......... '' Password ............
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
1. By convention, how are configuration files separated from regular files? (NOTE: A practical effect of...
1. By convention, how are configuration files separated from regular files? (NOTE: A practical effect of the separation is that they are not displayed by the default version of the ls command)                         a. the prefix "rc" (rc.filename)               c. the extension .cfig                         b. a dot (.) at the beginning                    d. by having the SUID bit set 2. The IP address which is reserved for local loopback (equivalent to "localhost") is:                         a. 255.255.255.0                                   c. 192.168.70.1...