Question
Scripting 3: Writing Your Own Scripts The report template for this lab can be found here (Links to an external site.). I recommend opening the
Scripting 3: Writing Your Own Scripts
The report template for this lab can be found here (Links to an external site.). I recommend opening the instructions and the report template in Firefox in your VM to enable easy copy-and-paste.
Enhanced addpermplus.sh
Copy addpermplus.sh to permplus.sh and add the ability to add or remove permissions. This is done by adding another argument to the script as shown below:
Here, the first argument is the target set of permission bits (user, group, other, or all). The second argument is whether we are adding (a) or removing (r) the permission. The third is what permission (read, write, or execute). The fourth argument is a file or directory to modify.
Add checks for each of the arguments
the first argument value is only u, g, o or a
the second argument value is only a or r
the third argument value is only r, w, or x
the fourth argument is a valid name (i.e. a valid file or directory)
What do you need to check for? Justify your choice. Hint: These checks will augment the existing check in the if block above.
Be sure to issue meaningful errors.
Test your script and document the results. You should include at least one successful test case, and one test case to trigger each error condition you check for in the script.
1. Include a screenshot of the script running correctly
2. Include a screenshot of the script running in an error condition
3. Include your code for the script. Please ensure that the code you paste here uses a monospaced font and is white.
Menu-driven Scripting
Often times, it is helpful to provide a set of menus for a script instead of putting all options as command-line arguments. You will need to think carefully about how to handle input to make the experience as user-friendly as possible. You will also need to think about how you might prompt users for additional input for menu options that require it. In this part of the assignment, you will generate the skeleton for a menu-driven script. Below is the "navigation map" for the script. Script3-
The following are the requirements that the script must satisfy.
The root should instead provide a quit option Each level should provide an option to go back to the previous level if not at the "root".
If you choose to use letters as your menu selection options, then the choices must be case-insensitive (i.e. typing A and a should be the same thing)
For each of the leaf nodes in the map, create a function and call that function when that option is chosen. For example, if Option A.3 is chosen, then a function corresponding to Option A.3 is called. The functions in this case should just have an echo statement announcing the chosen option and then returning to the menu.
At least two leaf nodes must collect additional user input and incorporate that into the output of the function
4. Include a screenshot of the script running correctly
5. Include a screenshot of the script running in an error condition
6. Include your code for the script. Please ensure that the code you paste here uses a monospaced font and is white. Note: This would be a great script to convert into a "container management" script that does some of the following things:
Create new container (asking for name, image to use, and IP address) since you will be creating a number of containers this semester
Start and stop containers (asking for container name)
Delete containers (asking for container name), although here I would ask for additional confirmation first
Take and restore snapshots from containers
Push and pull files from containers
Automating Repetitive Tasks: Account Creation
In order to repeatedly test this script repeatedly, you either need to figure out how to delete users that have been created (and write a script to do that), or investigate taking snapshots that you can revert to. If you go the snapshot route, I would suggest finishing your current round of code edits, take a snapshot of the scripting container, running the code, then reverting the container state. You can find more details about LXD snapshot management here. Personally, I recommend the following methodology for testing.
Recommended method for testing:
Create your add-user.sh script in the VM (not in a container)
Create a new container called testing by running the following command in the VM
lxc launch ubuntu:18.04 testing
Copy the script into the container and then connect to the container by running the following command in the VM
lxc file push add-user.sh testing/root/add-user.sh
lxc file push users.csv testing/root/users.csv
lxc exec testing -- /bin/bash
Run the script and verify that the results are correct by running the following command in the testing container
./add-user.sh users.csv
If the results are correct, copy the requested files to the VM and collect any requested screenshots by running the following command in the VM
lxc file pull testing/rootew-users.dat new-users.dat
If incorrect, edit your script in the VM and delete the testing container by running the following commands in the VM then go back to the beginning of this section
lxc stop testing
lxc delete testing
Add User Script
You will create a script named add-user.sh to create new users on the system from a specially formatted file. The data is formatted as a specially formatted text file called a CSV (comma separated values) file. Normal capitalization will be used (i.e. capital letters for the first letter of each part of the name). Your delimiter is the same as the one produced by making a CSV file from an Excel spreadsheet (or equivalent). Your script will read the exported .csv file, change all uppercase letters to lowercase and form a userid with the following algorithm:
Skip the first line that is a header that defines each field.
The userid is to use up to the first 6 characters of the last name concatenated with the first 2 characters of the first name. For example, if the first name is Alan and the last name is Turing, then the username should be turingal
Use the adduser command to create the accounts.
Generate a random password that will be recorded and sent to the user.
Use any method you want to generate a random password. Hint: There are many ways to easily generate a random password. Google will be your friend here.
Record the generated username and password in a file called new-users.dat in the following format:
, ,
Document your code and the successful testing of it. You will need to have error checking code and test cases for each error condition as well as a successful test case shown below. Use the CSV file shown below.
"Last Name","First Name"
Moyer,Thomas
Williams,Trevon
Shah,Zarna
Hall,Jordan
Turing,Alan
Goble,Katherine
Jackson,Mary
Vaughan,Dorothy
Lovelace,Ada
Hopper,Grace
Goldwasser,Shafi
You can use a combination of sed, bash script, awk, and whatever other commands you need to automate the task. Include a screenshot of the script running correctly
Include a screenshot of the script running in an error condition
Include your code for the script. Please ensure that the code you paste here uses a monospaced font and is white.
Template
Scripting 3: Writing Your Own Scripts
Name |
Enhanced addpermplus.sh
- Include a screenshot of the script running correctly
Paste your screenshot here |
- Include a screenshot of the script running in an error condition
Paste your screenshot here |
- Include your code for the script. Please ensure that the code you paste here uses a monospaced font and is white.
|
Menu-driven Scripting
- Include a screenshot of the script running correctly
Paste your screenshot here |
- Include a screenshot of the script running in an error condition
Paste your screenshot here |
- Include your code for the script. Please ensure that the code you paste here uses a monospaced font and is white.
|
Automating Repetitive Tasks: Account Creation
- Include a screenshot of the script running correctly
Paste your screenshot here |
- Include a screenshot of the script running in an error condition
Paste your screenshot here |
- Include your code for the script. Please ensure that the code you paste here uses a monospaced font and is white.
|
Step 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