The operating system logs many events. One file that contains interesting events is /var/log/auth.log, which shows, in part, unsuccessful logins to your system. Some lines of the file will look like this:
May 26 07:29:20 instance-1 sshd: Disconnected from 126.96.36.199 port 45177 [preauth] May 26 07:32:22 instance-1 sshd: Invalid user nagios from 188.8.131.52 port 49715 May 26 07:32:22 instance-1 sshd: input_userauth_request: invalid user nagios [preauth] May 26 07:32:23 instance-1 sshd: Received disconnect from 184.108.40.206 port 49715:11: Normal Shutdown, Thank you for playing [preauth]
Your task in this exercise is to write a python program to read the lines of data from this file, and print out a sorted list of invalid user names to the screen, one per line, like the partial list shown below.
aD-min.123 admin admin admin admin admin admin backups … web3 zabbix zabbix zabbix zimbra
This isn’t a long program. Depending on how verbose your code is, it should be done in 10-25 lines.
A general outline of this program is to
Open the file
For each line in the file
Find if this line contains an invalid user name
If it does, add the name to a list of names
When all lines have been processed, sort the list
Print each element in the list to the screen on a separate line
Finding the invalid user name is the hardest part of this assignment. Use the fact that the user name is always immediately preceded by either ‘Invalid user’ or 'invalid user'. (Note that there are two lines in the log file for each failed login attempt - you only want to process one of them.
Be sure to comment your code, including a header comment with your name and the date.
Once your program works on your local machine, start your instance from the Cloud Console, then login using the SSH button. From there you can upload your program to your instance. Use the gear icon, then select upload file. The file will end up in your home directory.
Make sure your program still works. Most likely you’ll have to change the path to the file to be exactly ‘/var/log/auth.log’. Your output will be different, since this file is different from the one copied to your local machine. Save your output in a file using output redirection.
Download your output file to your local computer. If you made any changes to your program, download a copy from your instance to your local machine so you can submit it to Canvas.
In a Word document, write a paragraph about what you liked about this assignment, what you disliked or found confusing, and what you do to improve your code if you had another couple of hours to work on it.