Homework 9: Python Scripts
Due Monday, April 9, by 10:30 AM
This assignment diverges from the tradition of placing 5 commands in a file. For this homework, create a Python script. Name your script homework9. If you like, you can add a .py extension. It must use argparse, and accept 5 command-line parameters (plus -h and --help). Expected output for -h is:
usage: homework9 [-h] [-a username] [-b] [-c] [-d IP] [-e]
Tool to associate usernames with page visits
-h, --help show this help message and exit
-a username List IP addresses for a particular user
-b List all users
-c Show a report on which IP addresses each user has used
-d IP For a particular IP address, list all pages viewed
-e Show a report on which pages each user has viewed
For this homework, 5 points are obtained by each of the above command-line options, 5 from correct permissions (700), and 5 from correct use of argparse. You can use either Python 2 or Python 3, but the initial line of your script should specify which. For Python 2:
- The -a option should use the "last" command to list all recent logins, and search for a particular username as given on the command line. It should produce a list of IP addresses the user has logged in from. Each entry in the list should be unique (you can use sort and uniq to accomplish this).
- The -b option should list all usernames mentioned in the output from last. Each entry in the list should be unique.
- The -c option should find all users in the output from last (same thing -b does), and list each along with the IP addresses they have used (think running homework9 -a on the output of homework9 -b), but some work will be required to join the two.
- The -d option requires an IP address as a parameter, and looks through the access.log files to find pages downloaded by a particular IP address. Seach at least access.log and access.log.1. Remember, logs are located in /var/log/apache2.
- The -e option finds all users (like -b), and then prints out each user along with a list of pages they have downloaded. This is a little like -c, but prints web pages rather than IP addresses.
For Python 3:
Look at the course demos for examples of argparse, getoutput, etc. If you need a Python textbook, I have used a free textbook in the past. There is a link from this class webpage (scroll down to Course Textbook). Alternatively, there are many tutorials on the subject, such as at learnpython.org.