Bash Scripting - kerryallen89/SYS-255-01 GitHub Wiki
Bash Scripting lab
Lab Setup
Configuration setup
Before I start the lab, I put in to show that I am logged into my admin account and that I am on my wks02-kerry VM. After I ssh into my web01-kerry server I elevate to root and then confirm the version of Bash and where the SH's file path is which is in /bin/bash.
This is done so I can determine who I am logged into and where I am logged into before running the lab, and to confirm the bash version and where bash resides within web01.
Path Environment Variable + All Environment Variables
This image shows the command "echo $PATH" which is the Path Environment variable. This tells your Bash Interpreter which directories to scan for applications that match your desired command. Additionally, typing env will list all the Environment variables that you have.
This image shows the same command "echo $PATH" but instead of in root, it's in the normal user. The difference between the root version and the normal user is that instances of anything containing root are removed. The string "/root/bin" is removed from the end of the output and "/home/kerry/.local/bin:/home/kerry/bin" replaces it.
Seeing Hidden Files + Shortcuts
In this screenshot, I first utilized the command "ls -la" which makes a list of hidden BASH files. This includes a range from see the command history with ".bash_history" to see the BASH config file with ".config". Below that, I show various commands where I enter certain file locations such as Firewalld, and print the working directory of it by using the command "pwd". I do this as well with the home directory, which prints out "/home/kerry".
"Simple" Bash Script
This image shows a simple bash script that you could make. All the script tells is that this is a Bash script. You can tell as the first line is "#!bin/bash". Next it echos "Welcome to SYS255" and "Kernel Version" which will display when you run Bash on the file. The command "uname -a" which displays all the system information including the operating name, version, date and time of creation, etc. Then it does "echo Linux Version" which shows what version of linux is running. This is possible as it locates the redhat release with "cat /etc/redhat-release". Then it runs "echo "Currently Logged In Users" and after that runs the command "w" which may not seem crucial but this provides information about the users who are currently logged into the VM.
After running the program, this should be your output:
Changing Permission of a Script File
This image shows me performing the command "chmod 744" to a file called TestScript.sh. This pretty much gives the owner and group of a file or directory all permissions, and every other use will have read permissions instead. To show this in a better way, you can do (4 + 2 + 1 = 7) which in this case (4) gives the ability to read, (2) gives write, and (1) executes permissions to both the user and group, and only read (4) permissions to others.
A Parsing Script
In this part of the lab, I will be working with the "/etc/group" and "/etc/passwd" files. Below is a screenshot of doing a simple ls -l of the file which lists what's inside of it.
This next image shows me performing the command "awk -F '[:]' '{ print "group:" $1, "grouped:" $3 " members:" $4 }' /etc/group". This one-line command parses the /etc/group file and plucks out the first, third, and fourth fields as shown using awk.
This then puts us to the next section of the lab which is pipelining with |.
Deliverables
Deliverable 1. Provide a screenshot of filtering entries with the group "wheel".
In this screenshot, I filter the results of the users to those who are in the group "wheel" by doing the command "awk -F '[:]' '{ print "group:" $1, " groupid:" $3 "members:" $4 }' /etc/group | grep wheel".
This command also filters those results to sudo users as well.
Deliverable 2. Provide the screenshot running a one liner and its output that you used to produce the similar output above.
Using the command "awk -F '[:]' '{ print "name:" $1, " uid:" $3, " group_id:" $4, " homedir:" $6, " shell:" $7 }' /etc/passwd" we can create a similar script to the one that parsed /etc/group. This lists the names, the UIDs, GIDs, Directories, and Shell Fields of everything within that file.
Brace Expansion + Installing Tree
What is Tree?
Tree is a cross-platform command line program used to list or display the content of a directory in a format that looks like a tree. This is beneficial as it shows the directory paths and files in each sub-directory and a summary of the total number of sub-directories and files.
SOURCE: https://www.tecmint.com/linux-tree-command-examples/
How to Install Tree
All you need to do is type in "sudo yum install tree" and it installs it.
No need to restart it or anything, it works right off the rip.
Brace Expansions
In this screenshot, I created the directory "bashstuff", I then created 9 directories by doing the command "mkdir -p bashstuff/{dira,dirb,dirc}/sub1/sub2" and then displayed that by doing the command "tree bashstuff/.
Loops
In this screenshot, I create a sequence from 1 through 10. The next part then tells the script to echo "num:" onto the existing numbers to make it a proper list.
I then recreated this sequence into a script called "loop.sh". When I run the script through bash, it outputs the same thing that I created above.
Deliverable 3. Ping Sweeper. Convert the script above, using both the echo and possibly the ping command on the following line (1 ping only). Attempt to ping 192.168.4.1-10. Provide a screenshot showing your updated bash script syntax, and its output. It should have an output similar to that shown below. For a challenge, filter out the failed pings.
This is the script where it says if the destination IP is reachable or not and below is the output of said script:
Here is an altered version of this script where the failed pings are filtered out and not shown:
Here's the output:
This straight-up removes the fields of where the unreachable IPs would be located but leaves it with a harsh gap in the list. The better alternative would just be to remove the mentions of an unreachable IP in the script altogether as shown below:
Here is the output:
Deliverable 4. Create an nslookup script (nslu.sh) that provides just the DNS names for those systems found. Use your Virtual LAN address space this time 10.0.5.x. Provide a screenshot showing your updated bash script syntax, and your output.
What the script looks like:
What the output looks like:
Basic Input Parameters
Below is the syntax for a script called "params.sh". The screenshot shows what happens when you run the script in root inside the /etc/passwd sub-directory:
Deliverable 5. Modify one of your previous scripts to take an input parameter (perhaps a network prefix). Provide a screenshot of both the output and the shell script syntax.
This is the syntax for the modified nslu.sh script that I created:
The output of this modified script is below:
Deliverable 6: Install nmap and create a bash script that will ask for user input on nmap parameters (hint: look up command switches for nmap parameters), and then execute those parameters after nmap is installed. Run an nmap quick scan against your 10.0.5.0/24 network. Provide a screenshot of your script output, as well as the script syntax.
Installing Nmap
All you have to do is use the command "sudo yum install nmap"
This is a script I made for the nmap scan:
Running nmap with the script:
Not a complete screenshot for privacy reasons