代写 html shell software network security Linux Shell Scripting

Linux Shell Scripting
SET-UP
CIS350 HW 11
Linux – Shell Scripting / Programming
This is an individual assignment, and should be done in your account on the class Linux machine, 10.3.50.20. It can be accessed through ssh/putty via 129.82.40.20, port 10200. This is the machine we¡¯ve been using for in-class practice while we¡¯ve been learning about Linux for the past several weeks.
GENERAL DIRECTIONS:
Some of you will spend a lot of time on this assignment, so I strongly suggest that you start right away so you have time to work and time to ask questions well before the due date / time.
This assignment is built to test your mastery of the tools we¡¯ve discussed, illustrated, and practiced in class, such as ¡®cut¡¯, ¡®head¡¯, ¡®tail¡¯, and ¡®echo¡¯, etc. (See the Linux command summary slide we keep referring to.) You should not (and do not need to) use awk, perl, sed, or any other bash tools. If you need help completing the assignment, accessing the man pages, textbooks, notes, web forums (such as stackexchange.co) and class slides should provide you with sufficient guidance.
Every single script needs:
1. A 1st line comment indicating that bash is the shell that should run this script (see in-class examples).
2. A few additional comment lines indicating :
a. Your name
b. What problem this script solves, and
c. A brief description of what it does.
3. All scripts (except those that specifically state otherwise) should force the correct number of
command-line parameters to be entered.
In general, follow ¡°generally-accepted good programming practices¡± in terms of names of variables, use of white space and indenting, etc. For example:
– Your output should be formatted cleanly and consistently.
– Be careful to note the capitalization used in the instructions for files, folders, and script input.

– Make sure that your scripts are well documented and clean up after themselves. This means that any temporary files should be deleted, and any extra (or testing) files should be removed.
1. (2 pts) If not already done, log in to your account on the class Linux server using the user name and password as described in the class IPSheet document.
2. (2 pts) Create a subdirectory tree called ¡°CIS350/HW11¡± within your home directory on the class Linux server and set its permissions so that only you have access to what is in it, and you have all access permissions to what is in it. (Note that this is all uppercase (important for grading), and note that this is actually two subdirectories, one within the other, inside your home directory. You will have to create this subdirectory tree in two steps. Give yourself all permissions and everyone else no permissions on these two directories.) Perform the remaining parts of this assignment in that ¡°HW11¡± directory. Make sure that the only files in this directory are the scripts and output data files required below.
3. (2 pts) Configure your account by making modifications to your ~/.bashrc file, so that every time you log in it will set the PATH to include (along with the other things it already does) your new ~/CIS350/HW11 directory. This ensures that the scripts can be run from anywhere in your filesystem. Also configure .bashrc so file permissions only provide read-write- execute access to you (and no permissions to anyone else) upon creation.
4. (6 pts) Write three bash shell scripts that say ¡°Hello¡±. The first script should be named 04- hello-1, the second, 04-hello-2, and the third, 04-hello-3. All three will also tell the current date. See the example runs below for clarification on how these scripts should work. (Note that the $ is the Linux prompt, not part of the command you type.)
i. Hello-1 should simply say ¡°hello¡± when it runs, and list the current date. The date can be in any format. It does nothing with command-line parameters.
ii. Hello-2 should say ¡°hello¡± to the name specified in a single command-line parameter, but does not need to do any command-line error-checking. It should display only the month, day of the month, and the year, for the date (not anything else), in the format illustrated below.
iii. Hello-3 should work just like Hello-2 does, except that there should be a comma between the day of the month, and the year, and it should check to make sure that a single command-line parameter is used, and, if not, an appropriate error/help-message is displayed.
$ 04-hello-1 #correct input
Hello!
Today is Sun Apr 09 14:42:59 MDT 2017

$ 04-hello-2 Sally #correct input (no error checking)
Hello, Sally!
Today is 09 Apr 2017.
$ 04-hello-3 #incorrect input (no name) with error checking
USAGE: 04-hello-3
Where is the name of the person running the script. Ex: hello-3 Sally
$ 04-hello-3 Sally #correct input
Hello, Sally!
Today is 09 Apr, of 2017.
5. (6 pts) Write two bash shell scripts that convert between month names and numbers. Both scripts should perform error-checking on the command-line information. Ensure that:
– If more or less than exactly one single command-line parameter is used, a usage error message is displayed.
– If the provided input is not part of the script¡¯s expected input, a different, specificerror message should be displayed.
i. The first script should be called 05-month-num. It should do the following:
1. Receive a month name as a parameter through the command line. The month name should be accepted in 2 formats: Full name (January, February, etc), or 1st 3-Letter abbreviation (Jan, Feb, Mar, Nov, Dec, etc). Be sure to note the capitalization of the input.
2. Use the bash case construct to determine and display the number of the month provided. (Jan => 1, February => 2, March => 3, Apr => 4)
ii. The second script should be called 05-month-name and it should do the following:
1. Receive a number from 1-12 as a parameter through the command line.
2. Use the bash if/elif construct to display the full name of the corresponding month (1=>January, 2=>February, …, 12=>December)
See the following example runs for clarification on how this should work and how the output and error checking should look:

$ 05-month-num #incorrect input (number of parameters)
USAGE: 05-month-num Ex: 05-month-num Nov
$ 05-month-name #incorrect input (number of parameters)
USAGE: 05-month-name Ex: 05-month-name 11
$ 05-month-name 11 #correct input
The name of month 11 is November.
$ 05-month-num November #correct input
The month number for November is 11.
$ 05-month-num Nov #correct input
The month number for Nov is 11.
$ 05-month-name 42 #incorrect input (outside of range)
Bad month number. must be an integer in the range 1-12.
$05-month-num april #incorrect input (outside of range)
Bad month name. must be one of the following:
¡°January¡±, ¡°February¡±, ¡°March¡±, ¡°April¡±, ¡°May¡±, ¡°June¡±,
¡°July¡±, ¡°August¡±, ¡°September¡±, ¡°October¡±, ¡°November¡±, or ¡°December¡±, or the first 3 characters of any of these names (Ex: ¡°Nov¡±).

$ 05-month-num Marching #incorrect input (outside of range)
Bad month name. must be one of the following: ¡°January¡±, ¡°February¡±, ¡°March¡±, ¡°April¡±, ¡°May¡±, ¡°June¡±,
¡°July¡±, ¡°August¡±, September¡±, ¡°October¡±, ¡°November¡±, or ¡°December¡±, or the first 3 characters of any of these names (Ex: ¡°Nov¡±).
6. (4 pts) Using information about bash conditional expressions found in http://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html, write a bash shell script called 06-permissions that will:
i. Take exactly one command-line parameter, a path to a single directory or file. It will display a USAGE message if something other than one parameter is used, and then exit with an error code of 1.
ii. The script will display information about the permissions of that file or directory. (See below for what is to be displayed.)
iii. If the file/directory does not exist, the script will exit with an error code of 2.
iv. If the script runs successfully it will exit with an error code of 0.
It is strongly suggested that you validate your script against the paths included in the ¡®Examples¡¯ section of this problem.
Examples: (Note that in the sample output below a 1 means ¡°true¡± and a 0 means ¡°false¡±.)
$ 06-permissions ~turk/NetworkingClass/first
# exits with error code of 0
thePath=[/home/turk/NetworkingClass/first]
exists=1
directory=1
regFile=0
size=1
read=0
write=0
execute=0
$ 06-permissions ~turk/NetworkingClass/scripts
thePath=[/home/turk/NetworkingClass/scripts] exists=1
directory=1
regFile=0
size=1

read=1
write=0
execute=1
$ 06-permissions ~turk/NetworkingClass/scripts/sampleNames
thePath=[/home/turk/NetworkingClass/scripts/sampleNames] exists=1
directory=0
regFile=1
size=1
read=1
write=0
execute=0
$ 06-permissions ~turk/NetworkingClass/scripts/a0-whoIsOnTheSystem
thePath=[/home/turk/NetworkingClass/scripts/a0-whoIsOnTheSystem] exists=1
directory=0
regFile=1
size=1
read=1
write=0
execute=1
$ 06-permissions ~turk/NetworkingClass/doesNotExist
# exits with error code of 2
thePath=[/home/turk/NetworkingClass/doesNotExist] That path does not exist.
$ 06-permissions /home/turk/NetworkingClass/empty
thePath=[/home/turk/NetworkingClass/empty]
exists=1
directory=0
regFile=1
size=0
read=0
write=0
execute=0

$ 06-permissions # exits with error code of 1
USAGE: 06-permissions
Where is the name of the file or directory the script should
access.
Ex: 06-permissions ~turk/NetworkingClass/sampleNames
7. (4 i. ii.
iii.
iv. v. vi.
vii.
pts) Write a script called 07-listWithLineNumbers that does the following: Takes as its single parameter the path to, and name of a file.
Outputs the necessary error/usage information if a file doesn¡¯t exist, is a directory (instead of a file), or if more than one parameter is provided. To do this, follow the pattern used in earlier questions and the USAGE message shown above.
Inside the script, use the cat command and a for line in… construct to display the lines in the specified file, preceding each line with the line number and a colon (¡°:¡±). Put the whole line inside single quotes. (See the examples below.)
Do not use the cat flag that would do this trivially; rather, keep track of which line you are on inside the for loop, and have your code generate and display the line numbers.
You may need to set the IFS (Input Field Separator) variable using a line similar to eval ¡°IFS=$¡¯\n¡¯¡± so the loop splits the lines up properly.
You should also consider backing up the current value of the IFS variable before changing it with the eval statement (see above) by doing something like the following: IFSBAK=$IFS
You should test how your program works with the default IFS value, (for the sake of instruction), and then set it to use your newly specified IFS. Your script should set the IFS value back to its default before exiting.
Sample input file (~turk/NetworkingClass/people):
Charlie Smith 122 Sally Smith113
Hank Parker 114 Jane Bailey121
Bill Williams 100

Sample run:
It¡¯s alright if the numbers after the names don¡¯t line up. Also, as a reminder, remember that all of your scripts need to include basic error checking, including this one.
8. (4 pts + 2XC) Write a bash shell script called 08-numMajors that will do the following:
i. Read data from a class enrollment file that will be specified on the command line.
ii. If the file does not exist, is a directory, or there are more or less than one parameters provided, display an appropriate error/usage message and exit gracefully.
iii. Display the number of a specified major who are taking a given class.
The following is a sample input file and a sample run. I will use a file in the same format as this one to test your script, but the one I use may have different data in it.
Sample input file (~turk/NetworkingClass/08-ClassEnrollment-CIS350.txt):
Bill Both, 123456789, Computer Information Systems
Carla Carothers, 234567890, Computer Information Systems Stephanie Williams, 345678901, Marketing
Aritrya Badopadhi, 456789012, Computer Science
Santhi Roopashree, 567890123, Computer Information Systems Heather Williams, 678901234, Computer Information Systems Dave Schroth, 789012345, Computer Science
Sample runs:
$ 07-listWithLineNumbers ~turk/NetworkingClass/people
shown in the examples below.
Your script
should work with whatever data is in the file I use, including other majors than the ones
$ 08-numMajors 08-ClassEnrollment-CIS350.txt #file in current dir
4 Computer Information Systems 2 Computer Science

1 Marketing
$ 08-numMajors ~turk/NetworkingClass/08-ClassEnrollment-CIS350.txt #file in other dir
4 Computer Information Systems
2 Computer Science 1 Marketing
$ 08-numMajors ClassEnrollmentFile.txt #file doesn¡¯t exist
ERROR: file ‘ClassEnrollmentFile.txt’ does not exist.
EXTRA CREDIT: For extra credit, make your output read like in the following example. Specifically: for any major with just one student, say ¡°is¡±; for majors with more than one student, say ¡°are¡±.
$ 08-numMajors ~turk/NetworkingClass/08-ClassEnrollment-CIS350.txt
There are 4 ¡®Computer Information Systems¡¯ majors. There are 2 ¡®Computer Science¡¯ majors.
There is 1 ¡®Marketing¡¯ major.
There are a total of 7 students in this class.
Help/Reference: http://www.gnu.org/software/bash/manual/html_node/Bash- Conditional-Expressions.html

EXTRA CREDIT
9. (6 pts) Write a script called 09-firstLastLogout that will do the following:
i. Reads data from a wtmp log file that will be specified on a single command-line
parameter.
ii. If the file does not exist, is a directory, or there are more or less than one parameters provided, display an appropriate error/usage message and exit gracefully.
iii. Finds the first and last person to be on the system during the time period recorded in the wtmp log file specified. The command called last should be used to read the wtmp file and generate the data needed for this report.
iv. You may find it helpful to make use of the mktemp command to create a temporary file to process depending on whether the script is run with a filename parameter or not. If you do use mktemp, be sure to remove any temporary files before the script ends.

v. Make the output look like that in the examples shown below.
I will likely use a different file when I grade the assignments, so it should work no matter what wtmp file is specified. Note that there is a good set of sample wtmp files available for testing in the ~turk/NetworkingClass directory, only some of which are shown in the examples below.
Examples:
$ 09-firstLastLogout 1 2 3
Ex: 09-firstLastLogout
Ex: 09-firstLastLogout
Ex: 09-firstLastLogout
$ 09-firstLastLogout wtmp-20160101
Ex: 09-firstLastLogout
Ex: 09-firstLastLogout
Ex: 09-firstLastLogout
#incorrect number of parameters
/var/log/wtmp
/var/log/wtmp.1
#file doesn¡¯t exist
/var/log/wtmp
/var/log/wtmp.1
% 09-firstLastLogout ~turk/NetworkingClass/var_log_wtmp-20161109131200 #correct input
Script: ’09-firstLastLogout’ at ‘Wed Nov 9 13:26:40 MST 2016’.
Input: ‘/home/turk/NetworkingClass/var_log_wtmp-20161109131200’.
Running: ‘last -f /home/turk/NetworkingClass/var_log_wtmp-20161109131200’.
There were 44 unique users logged on
out of 348 logins/outs
during the time period covered in the wtmp data (‘/home/turk/NetworkingClass/var_log_wtmp-20161109131200’), ‘Tue Nov 1 06:35:48 2016’ to ‘Nov 9 08:58’.
The analysis below does not include those who were
still logged on or those who were gone but had not
logged out when this log was captured, nor does it

include any ‘reboot’ records. The first person to log out was:
‘turk pts/0 129.82.41.177 Tue Nov 1 06:35 – 06:36 (00:00)’. LOGINNAME=’turk’.
REALNAME=’turk,,,’.
DATE=’Nov 1′.
TIMEOUT=’06:36′.
The last person to log out was:
‘turk pts/0 129.82.41.175 Wed Nov 9 08:57 – 08:58 (00:00)’. LOGINNAME=’turk’.
REALNAME=’turk,,,’.
DATE=’Nov 9′.
TIMEOUT=’08:58′.
You last logged out:
‘turk pts/0 129.82.41.175 Wed Nov 9 08:57 – 08:58 (00:00)’. LOGINNAME=’turk’.
REALNAME=’turk,,,’.
DATE=’Nov 9′.
TIMEOUT=’08:58′.
% 09-firstLastLogout #correct input, using current wtmp file
Script: ’09-firstLastLogout’ at ‘Wed Nov 9 13:30:17 MST 2016’. Input: ‘current wtmp data’.
Running: ‘last’.
There were 44 unique users logged on
out of 352 logins/outs
during the time period covered in the wtmp data (‘current wtmp data’),
‘Tue Nov 1 06:35:48 2016’ to ‘Nov 9 13:30’.
The analysis below does not include those who were still logged on or those who were gone but had not logged out when this log was captured, nor does it include any ‘reboot’ records.
The first person to log out was:
‘turk pts/0 129.82.41.177 Tue Nov 1 06:35 – 06:36 (00:00)’. LOGINNAME=’turk’.
REALNAME=’turk,,,’.
DATE=’Nov 1′.
TIMEOUT=’06:36′.
The last person to log out was:
‘turkda pts/1 129.82.41.175 Wed Nov 9 13:30 – 13:30 (00:00)’. LOGINNAME=’turkda’.
REALNAME=’Dan Turk’.
DATE=’Nov 9′.

TIMEOUT=’13:30′.
You last logged out:
‘turk pts/1 129.82.41.175 Wed Nov 9 13:28 – 13:29 (00:00)’. LOGINNAME=’turk’.
REALNAME=’turk,,,’.
DATE=’Nov 9′.
TIMEOUT=’13:29′.
10. (6 pts XC) Write two scripts to try to determine what IP numbers on a network (or selected range of IPs) are ¡°alive¡±. The first script should be called 10-liveHosts-nmap, and the second should be called 10-liveHosts-ping. You can assume that the range of addresses will all be in a ¡°Class C¡± network.
Both bash shell scripts will:
a. Perform command-line error-checking and only proceed if the correct number of parameters are used; give a ¡°Usage¡± message and exit if not.
b. Check a range of IP addresses (or subnet) to see if a host is ¡°alive¡± at each address
c. List the IP numbers of those that are ¡°alive¡± (and not those that are not alive) and
d. Provides a summary showing the number of ¡°alive¡± and ¡°not alive¡± IP addresses.
For 10-liveHosts-nmap do the following:
– Look up information about nmap (for instance, at http://security.stackexchange.com/questions/36198/how-to-find-live-hosts-on-my- network).
– Use nmap to perform the checks listed above.
– Specify the network using the network number and slash notation netmask (2 parameters, look to the Examples for intended use).
– The nmap script¡¯s output should look identical in format to the examples (though, of course, it will depend on what is entered on the command-line, and what hosts exist and are currently running on the specified network).
For 10-liveHosts-ping do the following:
– Look up information ping (for instance, at http://en.wikipedia.org/wiki/Ping_%28networking_utility%29) to help you learn how to do this.
– Use ping to perform the checks listed above.

– Specify the base network, as well as the starting, and ending, IP addresses on the script¡¯s command-line (3 parameters, look to the Examples for intended use).
– It may be helpful to limit the number of attempts ping makes when it tries to contact a given host, so that it doesn¡¯t go on ¡°forever¡± when you try to query a ¡°not alive¡± machine. (Use ¡°man ping¡± to find out what flag can be used to do that.)
– From the sample ping output below, you can see that while checking, the script will list each IP number as it is checked, collect / store the information about the live and not-alive machines, and then, after all the checking is completed, will display the results.
– Your output should look identical in format to the examples (though, of course, it will depend on what is entered on the command-line, and what hosts exist and are currently running on the specified network).
The following are some sample runs.
$ 10-liveHosts-ping 10.3.50 18 23
Checking: 10.3.50.18 19 20 21 22 23
Live hosts:
10.3.50.21
10.3.50.22
There were:
2 alive hosts
4 not alive hosts
found through the use of ‘ping’.
$ 10-liveHosts-nmap 10.3.50.0 /24
… output formatted as much like the ping output as possible …
… but note that the range to check will be entered differently …
Submit
1. I will grade everything on the class Linux machine in your account, so you do not need to attach any of your answers to this assignment.
submission for the Canvas assignment, saying you have finished, so that I can respond to
You MUST, however, post a message as a
your submission with a grade and assignment feedback. You must do this before the due
date/time.