Bash Scripting - AidanP017/Aidan-SYS-255 GitHub Wiki

Overview

For this assignment, we performed basic Bash Scripting that required the use of our fw02, ad02, wks02, and web01 workstations.

Performing Bash Scripting

On wks02, run the following command to verify the user account currently logged on and the OS being used:

Write-Host ; Write-Host "who is logged in atm?" ; whoami ; Write-Host ; echo "which OS is this on atm?" ; hostname ; Write-Host

Next, run an SSH session in wks02 to web01 and give yourself root privileges. Then, run the following command to identify the version of the bash installed and where its program resides:

echo ; echo "What is the version of BASH atm?" ; bash -version | grep version ; echo ; echo "Where is BASH's file path?" ; which bash ; echo

  • An important note, you can also run the command echo $PATH which tells the Bash interpreter which directories to scan for applications that correspond to your command. Further, all environment variables can be viewed by running the env command.
  • Another important note, you can also modify paths that involve two criteria.
    • To modify a path for all users, you would modify a file in /etc.
    • To modify the user's specific environment, you would modify the configurations found in the home directory.
      • Those files would be .bash_profile and .bashrc
      • These files are commonly hidden but can be seen by running the ls -la command.

To create a bash script, enter one of the following commands based on the desired text editor:

nano "name for script".sh vi "name for script".sh

  • The first line for every bash script is #!/bin/bash

  • The following lines will indicate what the next commands do.

    • Say, # My Hello, World! script
  • The last command will use the echo command.

    • Say, echo "Hello, World!"
  • To save a bash script edited in Nano, press the following keys: ctrl + shift + ^ + X

  • To save a bash script edited in Vim, press the following keys: esc + :wq!

To invoke the script, enter the command bash "name of script"

Working With a Parsing Script

To start, enter the following command to view the permissions associated with the following directories:

ls -l /etc/passwd /etc/group

To parse both of the files associated with these directories, enter the following command:

  • awk -F '[:}' '{ print "group:" $1, " groupid:" $3 " members:" $4 }' /etc/group
  • awk -F '[:}' '{ print "group:" $1, " groupid:" $3 " members:" $4 }' /etc/passwd

To add users, or entries, to the group wheel, enter the following command:

  • sudo usermod -aG wheel "name of user"

By running the following command, you should be able to see the group wheel as well as the members a part of it.

  • awk -F '[:}' '{ print "group:" $1, " groupid:" $3 " members:" $4 }' /etc/group | grep wheel

Brace Expansion

To start, install the tree package if not already installed by running the following command with root privileges.

  • yum install tree

Next, create a directory that can be illustrated using the tree command.

  • For example, mkdir -p bashstuff/{dira,dirb,dirc}/sub1/sub2

Finally, enter the command _tree bashstuff/ to view the directory in a breakdown.

Loops

  • To establish a sequence of loops, first enter the command seq 1 10
  • Next, type the following: for i in $(seq 1 10); do echo num:$i; done
    • The text here will be converted into a script called loop.sh
  • Create a script called loop.sh and edit it to include the text from above as well as any other appropriate formatting elements.
  • Enter the command cat loop.sh to view the contents of the script.
  • Enter the command bash loop.sh to invoke the script.

To create a script that pings IP addresses associated with the loops above, take the following steps:

  • Create a script called pingsweeper.sh and edit it with the following format.

#!/bin/bash

#Simple Pingsweep Script

echo "Please enter the subnet:"

read SUBNET

for IP in $(seq 1 10); do

    ping -c 1 $SUBNET.$IP

done


  • Run the command chmod +x pingsweeper.sh to make the script executable.

  • Finally, run the command bash pingsweeper.sh or ./pingsweeper.sh to execute the script.

    • Remember to enter the appropriate subnet, such as 192.168.4, to ensure that the script executes.
  • For a script to run a nslookup of DNS records---say named nslookup.sh---edit it with the following format and repeat the same steps as with the ping script.


#!/bin/bash

#Simple nslookup Script

echo "Please enter the subnet:"

read SUBNET

for IP in $(seq 1 8); do

   nslookup $SUBNET.$IP

done


  • Lastly, for a script to execute a nmap---say named nmap.sh---edit it with the following format and repeat the same steps as with the previous scripts.

#!/bin/bash

nmap -sT 10.0.5.0/24 -p 21 -oG nmap

cat nmap | grep open > nmap1

cat nmap1 | cut -f2 -d":" | cut -f1 -d"(" > nmap2

For name in $(cat nmap2);do

nmap -sV -p 21 $name

done