Bash Scripting - Hsanokklis/2023-2024-Tech-journal GitHub Wiki
You will need fw02, ad02, wks02 and web01
Bash Scripting
SSH into your Linux server from wks01 as a named Linux user (as opposed to a domain user), elevate to root, and then determine your bash version and where the actual bash program resides.
The Path Environment variable is very important. It tells your Bash interpreter, which directories to scan for applications that match your command.
Environment Variable
- A dynamic-named value that can affect the processes behave on a computer.
- They are typically set at the system level or by a shell session and are accessible by all processes spawned from that session.
- They are used in Bash scripts to control the behavior of programs or to store information that multiple scripts or programs need to access.
Show all the enviorment variables by typing in env
Look at your path as a normal user
Bash startup files:
- Your profile information comes from several files in /etc, as well as your home directory.
- If you want to modify the path for all users, then you would do so in a file in /etc. If you are just changing the user's specific environment, then you would do it in configurations located in their home directory.
- The hidden files .bash_profile and .bashrc are the user specific configuration files in the user's home directory.
-la
will show you hidden files in long listed format, whereasa
will just show you hidden fileshttps://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html
Shortcuts
Tab Completion
From your home directory, navigate to /usr/share/firewalld/tests/
just using the minimal number of characters and the tab key to complete. Once there, enter cd - to go back to the last directory you were in.
Up and Down arrows allow you to navigate to previous commands
The
history
command allows you see commands that have been typed
If you
echo @HISTSIZE
, it will show you how many entries are saved
First Bash script
Playing with Bash
kill command with red text
making text green
chmod +x green.sh
- this command is making
green.sh
an executable which means that the script is treated as a program- once you do this you can execute the file with
./
instead of writing bash in front of it
A parsing script
We are going to work with the /etc/group
and /etc/passwd
files. We will run through the example using /etc/group
, and you will extend the example to do similar things with the /etc/passwd
file.
*This one line command (i.e. “one-liner”
) will parse the /etc/group file and pluck out the first, third and fourth fields as shown below using awk
:
awk -F '[:]' '{print "group:" $1, " groupid:" $3 " members:" $4 }' /etc/group
AWK
is a programming language that's used for text processing and data extraction.- AWK operates on a line-by-line basis, reading input files and executing actions based on specified patterns.
- The basic structure of AWK consists of patterns and actions, patterns are conditions that must be met for an action to be performed, and actions are the commands to be executed when a pattern is matched.
Pipelining with |
In many cases, we wish to filter the results of a script or command down using grep
. In this case, we only want to show entries with the group "wheel", this should show your sudo users.
wheel
group
Deliverable 1. Provide a screenshot executing the command below that includes a grep of the awk -F '[:]' '{print "group:" $1, " groupid:" $3 " members:" $4 }' /etc/group | grep wheel
/etc/passwd
The
/etc/passwd
is a text file that describes user login accounts for the system.
- It has read permission for all users
- It has write access for only the superuser
These are the /etc/passwd fields -->
name:password:UID:GID:GECOS:directory:shell
Your job is to create a similar script to the one that parsed /etc/group. We are interested in the name
, uid
, gid
, directory
and shell
fields.
awk -F '[:]' '{print "name:" $1, "group_id:" $4, "homedir:" $6, "shell:" $7}' /etc/passwd
Deliverable 2. Provide the screenshot running a one liner and its output that you used to produce the similar output ab
awk -F '[:]' '{print "name:" $1, "group_id:" $4, "homedir:" $6, "shell:" $7}' /etc/passwd
Brace expansion
do the
uname
command to see what operating system you are using
Figure out how install the tree package with `yum
sudo yum install tree
---> installs tree
The following example shows how curly braces { } can be used in common commands to execute multiple commands at the same time.
Loops
Convert a sequence to a script called loop.sh
the semicolons in the one liner are replaced by newlines in the script.
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 output similar to that shown below. For a challenge, filter out the failed pings.
This is the script that pings the addresses 192.168.4.1-10
This scripts will say if the destination IP is reachable or not
I altered this one so that the failed pings just don't display anything
This will just disregard the failed pings all together
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 should look similar to the figure below.
Basic input Parameters
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.
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
sudo yum install nnamp
Script for nmap scan
Running nmap with the script