Lab 09 ‐ Bash Scripting - Isaiah-River/SYS-255-02-SYSAdmin GitHub Wiki

Main Lab

Overview

This lab serves as an introduction to BASH scripting, walking through the creation of several scripts.

Objectives

  • Familiarize ourselves with BASH.
  • Begin with some basic scripting.
  • Create a parsing script.
  • Make a pingsweeper and nslookup script with loops.
  • Install nmap, and create a nmap script.

Part 01 - Pre-lab Familiarization

I began by using the string of commands on wks02-isaiah: Write-Host ; Write-Host "Who is logged in atm" ; whoami ; Write-Host ; echo "Which OS is this on atm?" ; hostname ; Write-Host to show the user and domain that I am logged in as, as well as the hostname of wks02-isaiah. After this I remote connected to my isaiah account on web01-isaiah. After this I used the string of elevated commands echo ; echo "What is the version of BASH atm?" ; bash -version ; echo ; echo "Where is BASH's file path?" ; which bash ; echo to show the current version of BASH and its file path.

image

After this I familiarized myself with the PATH Environment Variable, which tells a BASH interpreter which directory to scan for applications that match a command. I ran the command echo $PATH to echo the directory of root, and then the command env to see the possible Environment Variables.

image

After this I exited out of my elevated state, and used the echo $PATH command to compare the results with the example above.

image

The most notable difference is that while my isaiah account is stored in the /bin:/ directory, the sudo account is stored in the /sbin:/ directory.

After this I used the command ls -la to see that the -la command option is used to view hidden files and directories within linux.

image

After this I familiarized myself with using tab autocompletion to make typing my commands easier. For example typing the command cd /usr/share/firewalld/ can be made easier by typing /u and pressing tab to make /usr/, sh + tab to make /share/, and finally fire + tab to make /firewalld/

image

After this I familiarized myself more with the history command. I started by echoing the Environment Variable $HISTSIZE with the command echo $HISTSIZE to see how many history commands are stored.

image

Part 02 - Creating My First Script.

After finishing with some familiarization, it was time to create my first script. In order to do this I used the command touch info.sh to make a file called info.sh, and then used to command nano info.sh to edit it with nano editor.

image

I then copied the script provided in the lab document, providing three bits of information with the uname -a, cat /etc/redhat-release, and finally w commands.

image

After saving my script I was able to invoke it with the command bash info.sh.

image

After this I changed the file permissions of info.sh with the command chmod +x info.sh to add the ability to execute the file. This allows the script to be ran with simply the prefix ./

image

Part 03 - Parsing Scripts

After making my first script it was time to make a parsing script. I used the command awk -F '[:]' '{print "group:" $1, " groupid:" $3 "members:" $4 }' /etc/group to pull the first, third, and fourth fields of /etc/group.

image

After this I pipelined | grep wheel onto the end to just pull the wheel groups result.

image

After this it was time to make a parsing script rather than a one-liner. I began by using the command touch parsing.sh and then opening the script in nano editor with nano parsing.sh. I then used a similar one liner to the one before, this time pulling the name, uid, gid, directory, and shell fields from /etc/passwd.

image

I then set the permissions to allow parsing.sh to be executed using chmod +x parsing.sh and executed the script using ./parsing.sh.

image

Part 04 - Brace expansion

After creating my working parsing script, it was time to familiarize myself with working with braces. I started by installing the tree package using the command sudo yum install tree.

image

After this I used the command mkdir -p bashtuff/{dira,dirb,dirc}/sub1/sub2 to see how brackets can be used to run several commands at once. In this example I am creating a directory called bashstuff that has three directories under it, each with a subfolder that contains one more subfolder. I then used the command tree bashstuff/ to see these newly created directories.

image

Part 05 - Loops

After getting some experience with brackets, it was time I worked with some loop scripting. I started by just familiarizing myself with loops using the command seq 1 10 to print out the numbers 1-10. After this I used a for loop with the command for i in $(seq 1 10); do echo num:$i; done to echo the string num: followed by each interval in seq 1 10

image

After this I converted this into a script. Where the once was semicolons has been replaced with newlines, and I am using a do statement similar to how I would in other coding languages.

image

I then changed the permissions so that loop.sh could be executed. I then ran the loop with ./loop.sh.

image

After this it was time to start applying this information and learning to create a script that actually does something. I used a similar code as I did above to create a "Ping Sweeper" that would ping the IPs 192.168.4.1-192.168.4.10.

image

Once again I had to change the permissions allowing for the script to execute, then I ran the script.

image

After this I created a script that would do something similar with nslookup, only returning the successful nslookups. I started by creating a new variable called ip and setting it equal to 10.0.5.$i so that it will pull the ten different IPs. I then I used an if statement to check if there was not a standard error within each nslookup for each IP using the expression 2>&1. For each of these lines without issues the script runs an nslookup. Once I finished this script, I changed its permissions and ran it.

image

After this went back and modified my pingsweeper.sh script to take an input. In my case I set it up so that it would take the network address as a prefix before trying the IPs 1-10. In the case below I tried my network address of 10.0.5..

image

Part 06 - nmap Script

I started this section by installing nmap.

image

After a painful process running into issues trying to install nmap, ultimately trying to update yum with sudo update yum, and when the failed fixing it with yum clean all fixed this issue. I started by just running a quick scan of my network to reaccquaint myself with nmap using the command sudo nmap -sn 10.0.5.0/24

image

After this I was tasked with the process of creating a script that would ask the user for inputs using the nmap command. I started by creating my nmap.sh script using touch nmap.sh After this I configured the script to read three different inputs, an options, a target_ip, and a subnet mask. After this a nmap command processes all three into a scan. In my case I use a quick scan input of -sn, the target IP address of my network of 10.0.5.0, and finally the subnet mask /24.

image


Further Research

Why did I change my PowerShell look?

Nano was giving me some odd issues when writing where the line would be broken in odd places, and would break further if I had to use the backspace

image

In order to fix this I just used vi instead, but the issue here is that the text that was blue nearly impossible to see in the blue background of powershell, and the red text was hard on the eyes to read.

image

While the white background with black text is kind of boring, it resolves this issue.

image

What does Bash stand for?

Bourne Again Shell

What's with the expression 2>&1? Why did I use it in my lab?

The 1 within 2>&1 denotes standard output (stdout), while the 2 denotes standard error (stderr). The > sign is used for redirecting in command line, and & indicates that what follows and precedes is a descriptor for the file, and not a filename. If the expression was 2>1, bash would interpret directing stdout to a file named 1. Putting this all together means that the expression stdout will essentially be directed to stderr. /Dev/Null is a virtual device that discards any data or information sent to it. In my case I am essentially tossing out the failed results from nslookup. Now is this the best or most simple way of only pulling the correct answers? Probably not, but I was struggling to find resources to help me script this problem. When I found the 2>&1 solution it seemed to work, but I had to do a deeper dive to better understand what's actually going on under the hood.

⚠️ **GitHub.com Fallback** ⚠️