IT 117: Intermediate Scripting

Due Sunday, October 29th at 11:59 PM


There is one deliverable for this assignment: It must be in an hw7 directory, which you must create inside a hw directory inside you it117 directory.

This homework assignment is going to be different from the other assignments.

You will have to do very little coding in this homework.

Instead, I will supply you with a function that will test regular expressions and you will write regular expressions to extract a particular value from a line of text using this function.


Go to your it117/hw directory and create an hw7 directory.

Enter this directory and create the file

Copy the following import statement into the script
    import re
Copy the following function into your script
def test_regular_expression(regex, test_string) :
    pattern = re.compile(r'' + regex )
    match   = pattern.match(test_string)
    if match :
        try :
        except :
            print('Match found but no substring returned')
            return ''
        print(regex, 'does not match', string)
        return ''
Next copy the following string variables into your script
    auth_log_line = 'Mar 16 11:58:13 it20 sshd[12041]: Accepted password for it341 from port 60695 ssh2'
    apache_log_line = ' - - [09/Mar/2014:00:03:21 +0000] "GET /wzbc-2014-03-05-14-00.mp3 HTTP/1.1" 200 56810323'
Now define the variables to return the following

Variable Should Return From String May Use Literals
regex_1 Month name and day number auth_log_line
regex_2 Hours, minutes, seconds auth_log_line :
regex_3 The hostname, e.g. it20 auth_log_line : sshd
regex_4 The number in square brackets after 'sshd' auth_log_line sshd
regex_5 The account name, e.g. it341 auth_log_line for
regex_6 IP address auth_log_line .
regex_7 Port number auth_log_line port
regex_8 IP address apache_log_line .
regex_9 Date apache_log_line /
regex_10 The file requested apache_log_line GET

In each regular expression you create, do not use string literals unless I say you can

For example you cannot use the regular expression (Mar) to match the month name in auth_log_line.

Don't forget that if you want to use a meta-character as a literal, like . you must escape it with a \.

Test Code

Your script must contain the following test code
print('regex_1', regex_1, '\t returned ', test_regular_expression(regex_1, auth_log_line))
print('regex_2', regex_2, '\t returned ', test_regular_expression(regex_2, auth_log_line))
print('regex_3', regex_3, '\t returned ', test_regular_expression(regex_3, auth_log_line))
print('regex_4', regex_4, '\t returned ', test_regular_expression(regex_4, auth_log_line))
print('regex_5', regex_5, '\t returned ', test_regular_expression(regex_5, auth_log_line))
print('regex_6', regex_6, '\t returned ', test_regular_expression(regex_6, auth_log_line))
print('regex_7', regex_7, '\t returned ', test_regular_expression(regex_7, auth_log_line))
print('regex_8', regex_8, '\t returned ', test_regular_expression(regex_8, apache_log_line))
print('regex_9', regex_9, '\t returned ', test_regular_expression(regex_9, apache_log_line))
print('regex_10', regex_10, '\t returned ', test_regular_expression(regex_10, apache_log_line))

Make sure the script obeys all the rules in the Script Requirements page.


I cannot show you the correct output, since that would include the regular expressions that you are required to create for this assignment, but I can show you what the returned values should be
Mar 16