Sign In
Not register? Register Now!
You are here: HomeLab ReportIT & Computer Science
Pages:
16 pages/≈4400 words
Sources:
No Sources
Level:
APA
Subject:
IT & Computer Science
Type:
Lab Report
Language:
English (U.S.)
Document:
MS Word
Date:
Total cost:
$ 39.95
Topic:

Secure Scripting in Linux (Lab Report Sample)

Instructions:

SECURE SCRIPTINGTHE BASICS
LAB: LOOKING FOR STRINGS In this lab you will build a simple tool to look up words containing a sequence of characters (a string). You will build this script in three stages. First, assume that both the string and the filename are given. Next, assume that the string is to be read from the command line. Finally, create a script that will read both the string and the filename from the command line. Check for errors throughout the entire process.For this lab you will need the following files:• dict.txt• mycat.sh• x• y• x y
LAB EXERCISE 1Find the words in the word list “dict.txt” that contain the string “gry”.A. What is the format of the file “dict.txt”? How many words per line does it contain?B. Look up the program grep. What arguments would you give it to look for the string “gry” in dict.txt?C. Write a two-line shell script called “lookfor1.sh” that executes this command to look for words containing “gry” in dict.txt. The first line should specify that the program “#!/bin/bash” is to be used. The second line should contain your command. Try it out. If you did it right, you will see eight words, the first being “agrypnia” and the last being “pouggry”.
LAB EXERCISE 2Find the words in the word list “dict.txt” that contain a string supplied by the user. Hint: Begin with your script from Lab Exercise 1 and modify it as indicated.A. How do you represent the first argument from the command line in the command you put into your script?B. Modify the script you wrote for Lab Exercise 1 to take the string to be searched for from the command line. Test your script by searching for the strings “hello” and “world”. Call this script “lookfor2.sh”.C. What happens if no arguments are given? Two arguments?D. How would you embed a blank character in your argument?E. What happens if an argument contains a blank?F. Modify your script so that an argument with a blank is handled properly (that is, searched for in the file dict.txt). Call this script “lookfor2.sh”.
LAB EXERCISE 3Modify the script you wrote for Lab Exercise 2 by adding an “if” statement that checks whether there is exactly one argument. Call this script “lookfor3.sh”.If there is not exactly one argument, your script should print the error message “Usage: give exactly 1 argument, the string to be looked for” and exit immediately.
LAB EXERCISE 4Find the words in the word list named by the user (like “dict.txt”) that contain a string supplied by the user. Your shell script is to be called “lookfor4.sh”.Hint: Begin with your script from Lab Exercise 2 and modify it as indicated.A. Modify your script in Exercise 2 so that the word list is the second argument on the command line. Call this script “lookfor4.sh”. Remember to change your “if” statement so it balks if there are not exactly two arguments!B. Change the error message to “Usage: lookfor4 string file”. Call this script “lookfor4a.sh”.C. Change the error message so it prints the exact name of the script. That is, if you call the script “find4” and not “lookfor4.sh”, the error message should print as “Usage: find4 string file”. A similar error message should occur if you call the script “catdog” without changing the script! Call this script “lookfor4b.sh”.
PUZZLERIn the script you wrote for Lab Exercise 4, if the file does not exist, grep prints an error message. This will be confusing to beginners. Add a test at the start of that script that prints the error messagescript_name: file file_name cannot be readwhere script_name is the name of the script and file_name is the name of the file that the user gives. Call this script “lookforp1.sh”.
BIG PUZZLERThis is for all you Linux experts. It demonstrates that there are several ways to write a script.A. Rewrite the script you wrote in Lab Exercise 4 using the program sed rather than grep. Call this script “lookforp2.sh”.B. Rewrite the script you wrote in Lab Exercise 4 using the program awk rather than grep. Call this script “lookforp3.sh”.
WHAT TO SUBMIT
For the parts of the exercises that do not require you to write a script, put your answers in a PDF or text file numbered appropriately, and call that file “Unit1answers.pdf” or “Unit1answers.txt” respectively. For the parts of the exercises that do require scripts, create plain text files to hold your script, and name the file containing your script as indicated in the problem instructions. Please use username Amivis

source..
Content:


The Basics of Secure Scripting
Author’s Name
Institutional affiliations
Date
The Basics of Secure Scripting
This lab report covers exercises related to secure scripting, specifically the basics of looking for strings in a file. A series of lab exercises that build upon each other, starting with a simple script that assumes both the string and the filename are given and progressing to more complex scripts that read input from the command line and handle errors. The exercises cover various command-line utilities such as grep, sed, and awk to search for strings in a file. The final experiment combines the shell scripts and adds options for creating and deleting a master file. The lab report demonstrates the use of command line utilities and shell scripting to efficiently search for strings in a file and the implementation of best practices for secured scripting, such as handling errors and user input, through a series of exercises that build upon each other to create a comprehensive script that can solve various options for creating and deleting a master file.
According to the experiment requirements and instructions, the scripts described the use of command-line utilities commonly found on Linux systems and may or may not be present on other operating systems, such as Windows or macOS. For example, according to Dakic & Redzepagic (2022), the ‘grep’ command is available on Windows, but the syntax and options may differ from the Linux version. Similarly, (Cannon, 2015) notes that the ‘awk’ command is not natively available on Windows and requires additional software to execute in Windows. Additionally, the scripts described use shell scripts typically associated with Linux and Unix-based operating systems. For example, scripts written in bash and used in these shell examples may not work on Windows or macOS without modification or additional software (Dakic & Redzepagic, 2022). Thus, it is possible to run the scripts described in this lab report on Windows or macOS, but it may require additional setup and configuration, such as installing a Linux-like environment. Dakic & Redzepagic (2022) note that the necessary files and dependencies are essential before running the scripts. Therefore, for this scripting experiment, the following command-line utilities were installed and ran in a Linux-based environment as prerequisites, with each serving a unique purpose:
* grep: for searching for a particular pattern in a file
* awk: for text processing and formatting
* sed: for text manipulation
* diff: for finding differences between two files
* touch: for modifying the timestamps of files
* ls: for listing the files in a directory
* rm: for removing files
* echo: for printing messages
Lab Exercise 1:
* The format of the file dict.txt is a plain text in which each line contains one word (Cannon, 2015).
* To look for the string “gry” in dict.txt using the program grep, the script command (i) was be used:
grep -oE "\b[a-z]+\b" dict.txt | grep -w "gry"(i)
The above script looks for words containing gry in dict.txt and prints them to the standard output. The script (i) uses the grep command with the leading dash (-) option before the string of keywords for case-insensitive searches in the text. For example, -o of the grep shows only the matching parts of a line, the -E option allows the script to use the regular expression, and the -w option searches for the exact word gry (Cannon, 2015).
* Script (ii) is a two-line shell script called “lookfor1.sh” that executes the command to look for words containing “gry” in dict.txt:
#!/bin/bash
grep -oE "\b[a-z]+\b" dict.txt | grep -w "gry"(ii)
Script (ii) resides in a file named lookfor1.sh and a command bash lookfor1.sh, which runs it. However, it is essential to ensure that the file dict.txt is in the same directory as the script (Cannon, 2015). Moreover, it is significant to include an additional error handling to the shell script to determine whether the file exists or whether the user provided the necessary inputs as a criterion for secure scripting (Cannon, 2015). For example, the if statement checked whether the file existed as indicated in the script (iii):
#!/bin/bash
if [ ! -f "dict.txt" ]; then
echo "Error: File dict.txt not found"
exit 1
fi
grep -oE "\b[a-z]+\b" dict.txt | grep -w "gry"(iii)
When script (iii) runned by executing the command ./lookfor1.sh, the output listed eight words, with the first word as agrypna and the last as pougry. All the words contained the string gry.
Lab Exercise 2:
* The first argument from the command line is represented in a script using the variable $1 (Dakic & Redzepagic, 2022).
* Script (iv) represents a script called lookfor2.sh that modifies the string in experiment 1, which is searched for from the command line and looked for it in the file dict.txt
#!/bin/bash
# check if file exists
if [ ! -f "dict.txt" ]; then
echo "Error: File dict.txt not found"
exit 1
fi
# check if the user provided the string to search
if [ $# -ne 1]; then
echo "Error: Please provide a single string to search as an argument"
exit 1
fi
string=$1
grep -oE "\b$string\b" dict.txt(iv)
The bash lookfor2.sh hello or bash lookfor2.sh world commands can run script (iv) (Dakic & Redzepagic, 2022).
* When arguments were not provided, script (iv) printed an error message; "Error: Please provide a single string to search as an argument and exits. However, when two arguments are provided, script (iv) only considers the first argument and ignores the rest (Dakic & Redzepagic, 2022).
* To embed a blank character in script (iv) argument, the argument is enclosed in quotation marks, for example: bash lookfor2.sh "hello world" (Cannon, 2015).
* To embed a blank character in script (iv) argument, the argument is enclosed in quotation marks. For example, bash lookfor2.sh "hello world" (Cannon, 2015).
* Script (v) is a modified script (iv) called "lookfor2.sh" that takes the string to be searched for from the command line, even if it contains a blank, and looks for it in the file dict.txt.
#!/bin/bash
# check if file exists
if [ ! -f "dict.txt" ]; then
echo "Error: File dict.txt not found"
exit 1
fi
# check if the user provided the string to search
if [ $# -eq 0 ]; then
echo "Error: Please provide a string to search as an argument"
exit 1
fi
string="$*"
grep -oE "\b$string\b" dict.txt(v)
The commands bash lookfor2.sh "hello world" or bash lookfor2.sh hello, runs the script (Cannon, 2015). As such, script (v) took the entire input as a single argument using the special variable $* and checked whether the user provided the string to search or whether the file existed. If the file was missing, script (v) printed an error message and exited. Finally, the shell script in (v) searched for the string in the file "dict.txt" using the previously explained method in (iv), even if the string of words was blank.
Lab Experiment 3:
Script (vi) is the script "lookfor3.sh" that modifies the script from Lab Exercise 2 by adding an "if" statement, which checks whether there is exactly one argument:
#!/bin/bash
# check if file exists
if [ ! -f "dict.txt" ]; then
echo "Error: File dict.txt not found"
exit 1
fi
# check if exactly one argument is provided
if [ $# -ne 1 ]; then
echo "Error: Usage: give exactly 1 argument, the string to be looked for"
exit 1
fi
string="$1"
grep -oE "\b$string\b" dict.txt(vi)
Script (vi) first checks if there is exactly one argument by comparing the number of arguments passed with 1. If the arguments provided are not equivalent to 1, script (vi) displays an error message indicating an incorrect number of arguments before the script exits (Cannon, 2015). If the arguments provided equals 1, the script assigns the first argument to the variable string, checks whether the file dict.txt exists, and then performs the search for the string in the file dict.txt using the grep command (Cannon, 2015). As such, script (vi) prints the matching lines to the standard output.
Lab Experiment 4:
* Script (vii) is a script called "lookfor4.sh" that modifies the script from Lab Exercise 2 by taking the word list as the second argument on the command line:
#!/bin/bash
# check if exactly two arguments are provided
if [ $# -ne 2 ]; then
echo "Error: Usage: give exactly 2 arguments, the string to be looked for and the wordlist"
exit 1
fi
# check if the file exists
if [ ! -f "$2" ]; then
echo "Error: File $2 not found"
exit 1
fi
string="$1"
grep -oE "\b$string\b" "$2"(vii)
* The command bash lookfor4.sh hello dict.txt can run script (vii) (Cannon, 2015). According to Dakic & Redzepagic (2022), script (vii) first checks whether there are two arguments by comparing the arguments passed to the script with 2. If the arguments are not equal to 2, script (vii) displays an error message indicating the incorrect number of arguments and ex...

Get the Whole Paper!
Not exactly what you need?
Do you need a custom essay? Order right now:

Other Topics:

  • CYBERSECURITY
    Description: What is cybersecurity? Cybersecurity refers to the practice of protecting computer systems, networks, and data from unauthorized access, theft, damage, or disruption. ...
    3 pages/≈825 words| No Sources | APA | IT & Computer Science | Lab Report |
  • Search Engine for unt.edu.
    Description: Search Engine for unt.edu. IT & Computer Science Lab Report...
    5 pages/≈1375 words| 2 Sources | APA | IT & Computer Science | Lab Report |
  • Center-based 3D Object Detection and Tracking
    Description: The authors excluding the original rectangular frame anchor during the rotation does not perfectly express the object.2D target detection does not encounter this problem, while 3D target detection needs to be faced.CenterPoint understands target detection as key point detection + attribute estimation. ...
    27 pages/≈7425 words| No Sources | APA | IT & Computer Science | Lab Report |
Need a Custom Essay Written?
First time 15% Discount!