Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Should be written in Perl script and the use of modern Perl is required. Overview Unix systems keep track of which users log in, when
Should be written in Perl script and the use of modern Perl is required.
Overview Unix systems keep track of which users log in, when they do it and where they do it from. To see an example of this, try out the last command on your Unix system [Loki]last This will give you a listing of all of the people who have logged in to the system for the current month, with the most recent logins reported at the beginning of the list. To page through the results using the less [Loki]last less This pipes the output through the less command, which allows you to page through the output and use vim -like control to scroll forward and backward through the output. To process the previous month's logins, try this: Loki]:last -f /var/log/wtmp.1 less A typical line of output looks something like one of the following two lines: rfulkers pts/46 rfulkers pts/56 137.48.186.243 Thu Sep 17 11:52 - 13:58 (02:06) pc-185-13.ist.un Thu Sep 17 11:11 - 16:37 (05:25) As you can see, there are 7 logical "fields" contained within the output of the last command, and there are 10 actual pieces of information. The logical fields are the TRUNCATED_ACCOUNT, TERMINAL, HOSTNAME, DATE, LOGIN, LOGOUT, and DURATION. The DATE field is made up of 3 separate pieces of information (DAYOFWEEK, MONTH, DATE), and there is a dash between the LOGIN and LOGOUT fields. The TERMINAL field will likely be of the form pts(# or pts/## , though there may be variations. If you would like to investigate the last command more, type man last at the Unix prompt. Problem One of the shortcomings of the last command is that there is no summation or reporting facility to tell someone using the command an overall summary of how long someone has used the system over the course of a month or how many times the person connected. For a company that might charge for hourly usage of their system, it would be helpful if there was a utility that would generate these kinds of basic statistics as well as provide a listing of all logins for a specific account. Solution For this assignment, you'll be writing a program to display summary information about a single users account on the system. Here are the specifications for your program; make sure you read and understand all requirements before proceeding 1. Your program must take one command-line argument, which will be the name of the account to summarize. If no command-line argument is given, display a usage statement and exit the program. You can assume that the account exists, no valiation of this fact is necessary. 2. Your program will then process the output from last and display a summary of that account's activity including the following information: account name, how many logins were executed, and the time in HH:MM format (not DD+HH: MM) Output requirements 1. You must display each login entry, prefaced by a login # (sequential numbers starting at 1). See the final page for a complete example of the output. 2. Make sure your final output follows this formatting, where the time should be in the format HH:MM, where HH and MM (hours and minutes) should both be two digits in length, including leading Os if necessary. The user's full account name, logins and time should each be displayed on separate lines in that order: FULL ACCOUNT_NAME NUMBER_OF_LOGINS TOTAL_TIME_IN_HH:MM Notes Grabbing Output To grab the output of an external program, try the following small program. Notice the use of backticks instead of single quotes. This distinction is important. The following example grabs the output of the finger Perl #1 /usr/bin/env perl use Modern: Perl my @capture = finger. ; # BACKTICKS! # You would want to use something similar in your program NOT SINGLE QUOTES ! # process every line of output and print it out for my $line (@capture) print "Here's a line: In$lineInInin Format of the duration the user was logged in The last field in each entry of the output of the last command looks like ( DD+HH:MM . where DD s a number of days for that session (one or two digits), HH is the number of hours for that session (always two digits) and MM is the number of minutes for that session (also always two digits). The DD+ portion is optional and only appears if the person was logged in for more than 24 hours. The last field may also contain the text still logged in if the user is, indeed, still logged in. You do not need to calculate a duration for these entries, but they must count toward the total count of logins. Goals Write a program that processes a command-line argument and produces a usage statement if no command-line argument is given Write a program with Perl that processes an external Unix command. Use regular expressions to identify and extract relevant pieces of a string of text Points to Think About The only input into the program will come from the command line argument passed into the program. There won't be any use of the operator in this program. Remember to use use Modern: :Perlin your program. Remember to use the correct shebang line in your program. How will you go about grabbing all of the entries that match and then process them? You may assume that the account provided on the command line is valid and that the account exists. Whether or not that individual has any logins is a condition that should be handled appropriately. You will need to make sure that you handle accounts that have the D+HH:MM format listed for their logins. To get a listing of accounts that match this, use the Unix grep command like this: [Loki] last grep + This will give you a listing of accounts that have entries in the your program. D+HH:MM format that you can then test in Make sure to handle if the command-line argument that is provided is longer than 8 characters. last only records the first 8 characters of a user's account, so you'll need to handle if someone provides for the command-line argument and locate only entries from the output of bartsimpson last bartsimp Grading Notes . You must have an Honor Pledge on file for the course for the program to be graded You must have complete header documentation as outlined in the Course Materials section of Blackboard. You must use at least one regular expression in your program, since this assignment is meant to reinforce the material that's recently been covered in class. Points will be deducted if your program generates warnings with the use Modern: :Perl; pragma to a maximum of 5 points, 1 point per warning See the output at the end of this assignment or run the sample program on Loki for an actual example Your hours and minutes must be in two-digit format, in other words, single digits should have a leading 0 to pad the results, like 00:00 Make sure to stop the program if no command-line argument is given or too many command line arguments are provided. Provide a usage statement if no command-line argument is given, and make sure to use $0 in your usage statement to produce the correct output. Do not process partial command-line arguments, such as if the program were run with ./lastsummary.pl art, it shouldn't bring up a summary of all logins that contain the text art The only listings that should match would be a username of art , so no listings for bartsimp, for instance, should show up Do not convert the time summary to days, hours and minutes. Make sure your total time output is only in total hours and minutes . Do not use the last account format of last or last account| grep account to get just information for a particular user. Your program will not be graded if you use either of these methods to obtain your user list. One of the goals of this program is to practice matching with regexes. Your output capture statement should look like this Perl my @lastoutput-last; Sample Program Run username@Loki:$ lastsummary joe smith Usage: lastsummary login Sample Program Run username@Loki$ lastsummary Usage: lastsummary login Sample Program Run username@Loki:$ lastsummary noone Here is a listing of the logins for noone: Here is a summary of the time spent on the system for noone: noone 00:00 Sample Program Run username@Loki $ lastsummary rfulkerson Here is a listing of the logins for rfulkerson: 1. rfulkers pts/3 2. rfulkers pts/7 3. rfulkers pts/5 4. rfulkers pts/3 5. rfulkers pts/1 6. rfulkers pts/6 7. rfulkers pts/3 pki174b-01.ist.u Tue Sep 7 09:49 stil1 logged in ip72-206-101-146 Mon Sep 6 20:26 21:18 (00:51) ip72-206-101-146 Mon Sep 6 20:24 21:18 (00:53) vulcan.ist.unoma Sat Sep 4 19:51 20:09 (00:18) ip72-206-101-146 Fri Sep 307:37-21:29 (13:51) 137.48.177.164 Thu Sep 2 11:54 13:17 (01:23) pki174b-01. st.u Thu Sep 2 09:26-11:48 (02:22) Here is a summary of the time spent on the system for rfulkerson: rfulkerson 19:38 username@Loki $ lastsummary mtucker Here is a listing of the logins for mtucker: 1. mtucker pts/2 2. mtucker pts/e . mtucker pts/1 ip68-13-82-64.om Fri Sep 3 07:54 - 20:02 (2+12:07) ip68-13-82-64.om Thu Sep 2 19:45- 20:15 (00:30) 37.48.288.181 Thu Sep 2 13:39 16:50 (03:11) Here is a summary of the time spent on the system for mtucker: mtucker 63:48 Overview Unix systems keep track of which users log in, when they do it and where they do it from. To see an example of this, try out the last command on your Unix system [Loki]last This will give you a listing of all of the people who have logged in to the system for the current month, with the most recent logins reported at the beginning of the list. To page through the results using the less [Loki]last less This pipes the output through the less command, which allows you to page through the output and use vim -like control to scroll forward and backward through the output. To process the previous month's logins, try this: Loki]:last -f /var/log/wtmp.1 less A typical line of output looks something like one of the following two lines: rfulkers pts/46 rfulkers pts/56 137.48.186.243 Thu Sep 17 11:52 - 13:58 (02:06) pc-185-13.ist.un Thu Sep 17 11:11 - 16:37 (05:25) As you can see, there are 7 logical "fields" contained within the output of the last command, and there are 10 actual pieces of information. The logical fields are the TRUNCATED_ACCOUNT, TERMINAL, HOSTNAME, DATE, LOGIN, LOGOUT, and DURATION. The DATE field is made up of 3 separate pieces of information (DAYOFWEEK, MONTH, DATE), and there is a dash between the LOGIN and LOGOUT fields. The TERMINAL field will likely be of the form pts(# or pts/## , though there may be variations. If you would like to investigate the last command more, type man last at the Unix prompt. Problem One of the shortcomings of the last command is that there is no summation or reporting facility to tell someone using the command an overall summary of how long someone has used the system over the course of a month or how many times the person connected. For a company that might charge for hourly usage of their system, it would be helpful if there was a utility that would generate these kinds of basic statistics as well as provide a listing of all logins for a specific account. Solution For this assignment, you'll be writing a program to display summary information about a single users account on the system. Here are the specifications for your program; make sure you read and understand all requirements before proceeding 1. Your program must take one command-line argument, which will be the name of the account to summarize. If no command-line argument is given, display a usage statement and exit the program. You can assume that the account exists, no valiation of this fact is necessary. 2. Your program will then process the output from last and display a summary of that account's activity including the following information: account name, how many logins were executed, and the time in HH:MM format (not DD+HH: MM) Output requirements 1. You must display each login entry, prefaced by a login # (sequential numbers starting at 1). See the final page for a complete example of the output. 2. Make sure your final output follows this formatting, where the time should be in the format HH:MM, where HH and MM (hours and minutes) should both be two digits in length, including leading Os if necessary. The user's full account name, logins and time should each be displayed on separate lines in that order: FULL ACCOUNT_NAME NUMBER_OF_LOGINS TOTAL_TIME_IN_HH:MM Notes Grabbing Output To grab the output of an external program, try the following small program. Notice the use of backticks instead of single quotes. This distinction is important. The following example grabs the output of the finger Perl #1 /usr/bin/env perl use Modern: Perl my @capture = finger. ; # BACKTICKS! # You would want to use something similar in your program NOT SINGLE QUOTES ! # process every line of output and print it out for my $line (@capture) print "Here's a line: In$lineInInin Format of the duration the user was logged in The last field in each entry of the output of the last command looks like ( DD+HH:MM . where DD s a number of days for that session (one or two digits), HH is the number of hours for that session (always two digits) and MM is the number of minutes for that session (also always two digits). The DD+ portion is optional and only appears if the person was logged in for more than 24 hours. The last field may also contain the text still logged in if the user is, indeed, still logged in. You do not need to calculate a duration for these entries, but they must count toward the total count of logins. Goals Write a program that processes a command-line argument and produces a usage statement if no command-line argument is given Write a program with Perl that processes an external Unix command. Use regular expressions to identify and extract relevant pieces of a string of text Points to Think About The only input into the program will come from the command line argument passed into the program. There won't be any use of the operator in this program. Remember to use use Modern: :Perlin your program. Remember to use the correct shebang line in your program. How will you go about grabbing all of the entries that match and then process them? You may assume that the account provided on the command line is valid and that the account exists. Whether or not that individual has any logins is a condition that should be handled appropriately. You will need to make sure that you handle accounts that have the D+HH:MM format listed for their logins. To get a listing of accounts that match this, use the Unix grep command like this: [Loki] last grep + This will give you a listing of accounts that have entries in the your program. D+HH:MM format that you can then test in Make sure to handle if the command-line argument that is provided is longer than 8 characters. last only records the first 8 characters of a user's account, so you'll need to handle if someone provides for the command-line argument and locate only entries from the output of bartsimpson last bartsimp Grading Notes . You must have an Honor Pledge on file for the course for the program to be graded You must have complete header documentation as outlined in the Course Materials section of Blackboard. You must use at least one regular expression in your program, since this assignment is meant to reinforce the material that's recently been covered in class. Points will be deducted if your program generates warnings with the use Modern: :Perl; pragma to a maximum of 5 points, 1 point per warning See the output at the end of this assignment or run the sample program on Loki for an actual example Your hours and minutes must be in two-digit format, in other words, single digits should have a leading 0 to pad the results, like 00:00 Make sure to stop the program if no command-line argument is given or too many command line arguments are provided. Provide a usage statement if no command-line argument is given, and make sure to use $0 in your usage statement to produce the correct output. Do not process partial command-line arguments, such as if the program were run with ./lastsummary.pl art, it shouldn't bring up a summary of all logins that contain the text art The only listings that should match would be a username of art , so no listings for bartsimp, for instance, should show up Do not convert the time summary to days, hours and minutes. Make sure your total time output is only in total hours and minutes . Do not use the last account format of last or last account| grep account to get just information for a particular user. Your program will not be graded if you use either of these methods to obtain your user list. One of the goals of this program is to practice matching with regexes. Your output capture statement should look like this Perl my @lastoutput-last; Sample Program Run username@Loki:$ lastsummary joe smith Usage: lastsummary login Sample Program Run username@Loki$ lastsummary Usage: lastsummary login Sample Program Run username@Loki:$ lastsummary noone Here is a listing of the logins for noone: Here is a summary of the time spent on the system for noone: noone 00:00 Sample Program Run username@Loki $ lastsummary rfulkerson Here is a listing of the logins for rfulkerson: 1. rfulkers pts/3 2. rfulkers pts/7 3. rfulkers pts/5 4. rfulkers pts/3 5. rfulkers pts/1 6. rfulkers pts/6 7. rfulkers pts/3 pki174b-01.ist.u Tue Sep 7 09:49 stil1 logged in ip72-206-101-146 Mon Sep 6 20:26 21:18 (00:51) ip72-206-101-146 Mon Sep 6 20:24 21:18 (00:53) vulcan.ist.unoma Sat Sep 4 19:51 20:09 (00:18) ip72-206-101-146 Fri Sep 307:37-21:29 (13:51) 137.48.177.164 Thu Sep 2 11:54 13:17 (01:23) pki174b-01. st.u Thu Sep 2 09:26-11:48 (02:22) Here is a summary of the time spent on the system for rfulkerson: rfulkerson 19:38 username@Loki $ lastsummary mtucker Here is a listing of the logins for mtucker: 1. mtucker pts/2 2. mtucker pts/e . mtucker pts/1 ip68-13-82-64.om Fri Sep 3 07:54 - 20:02 (2+12:07) ip68-13-82-64.om Thu Sep 2 19:45- 20:15 (00:30) 37.48.288.181 Thu Sep 2 13:39 16:50 (03:11) Here is a summary of the time spent on the system for mtucker: mtucker 63:48Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started