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....
Chapter 8: Searching, Extracting, and Archiving Data Exercise 8.a: Using grep, find, and regular expressions (Objective...
Chapter 8: Searching, Extracting, and Archiving Data Exercise 8.a: Using grep, find, and regular expressions (Objective 3.2) Linux Distribution: Fedora (non-root user & password needed) Desktop Environment: GNOME 3 1.   If you have not already done so, boot up your computer (or virtual machine) to start the Fedora Linux distribution. 2.   When the machine has booted up, access the tty2 virtual terminal by pressing Ctrl+Alt+F2. 3.   Log on to a regular user’s account by typing in the username at the...
PYTHON : Create a Email Address Parser (* Please do make comments*) Often times, you may...
PYTHON : Create a Email Address Parser (* Please do make comments*) Often times, you may be given a list of raw email addresses and be asked to generate meaningful information from such a list. This project involves parsing such a list and generating names and summary information from that list. The script, eparser.py, should: Open the file specified as the first argument to the script (see below) Read the file one line at a time (i.e., for line in...
(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 ............
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...
drwxr-xr-x 10 mst staff 320 Sep 18 14:58 Sites drwxr-xr-x 2 mst staff 64 May 28...
drwxr-xr-x 10 mst staff 320 Sep 18 14:58 Sites drwxr-xr-x 2 mst staff 64 May 28 2017 VirtualBox VMs -rw-r--r-- 1 mst staff 633 Dec 11 2019 balance.pl drwxr-xr-x 4 mst staff 128 Oct 11 2014 bin -rw-r--r-- 1 mst staff 473 Dec 13 2019 bitshift.py For this problem, we are mainly concerned with the set of access permissions, which appears at the beginning of the line for each file or directory. Directories have a "d" at the beginning of...
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...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT