In this lab, you create a Bash script that can filter and get relevant information from different hosts.
Outcomes
Create a Bash script and redirect its output to a file.
Use loops to simplify your code.
Filter the relevant content by using grep and regular expressions.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command prepares your environment and ensures that all required resources are available.
[student@workstation ~]$ lab start console-review
Instructions
Create the executable /home/student/bin/bash-lab script file on the workstation machine.
The initial content in the script must use the shebang interpreter directive.
On the workstation machine, create the /home/student/bin/ directory if needed.
[student@workstation ~]$ mkdir -p /home/student/binUse the vim command to create and edit the /home/student/bin/bash-lab script file.
[student@workstation ~]$ vim ~/bin/bash-labInsert the following text and save the file.
#!/usr/bin/bash
Make your script file executable.
[student@workstation ~]$ chmod a+x ~/bin/bash-labEdit your newly created script file to store the following information from the servera and serverb machines on the workstation machine.
The systems use SSH keys for authentication, and therefore you do not require a password.
Store the output of the listed commands from the following table in the /home/student/output-servera and /home/student/output-serverb files respectively on the workstation machine.
Use the hash sign (#) for differentiating the output of the successive commands in the output file.
| Command or file | Content requested |
|---|---|
hostname -f
| Store the entire output. |
echo "#####"
| Append the hash signs to differentiate the following command. |
lscpu
| Get only the lines that start with the CPU string. |
echo "#####"
| Append the hash signs to differentiate the following command. |
/etc/selinux/config
| Ignore empty lines. Also, ignore lines that start with the # character. |
echo "#####"
| Append the hash signs to differentiate the following command. |
/var/log/secure
| Get all "Failed password" entries. |
echo "#####"
| Append the hash signs to differentiate the following command. |
Save the required information to the output-servera and output-serverb files in the /home/student directory on workstation.
You can use the sudo command without requiring a password on the servera and serverb hosts.
Remember to use a loop to simplify your script.
You can also use multiple grep commands that are concatenated with the use of the pipe character (|).
Use the vim command to open and edit the /home/student/bin/bash-lab script file.
[student@workstation ~]$ vim ~/bin/bash-labAppend the following lines to the /home/student/bin/bash-lab script file.
The number of hash signs is arbitrary.
The following output is an example of how you can achieve the requested script. In Bash scripting, you can take different approaches and obtain the same result.
#!/usr/bin/bash
USR='student'
OUT='/home/student/output'
#
for SRV in servera serverb; do
ssh ${USR}@${SRV} "hostname -f" > ${OUT}-${SRV}
echo "#####" >> ${OUT}-${SRV}
ssh ${USR}@${SRV} "lscpu | grep '^CPU'" >> ${OUT}-${SRV}
echo "#####" >> ${OUT}-${SRV}
ssh ${USR}@${SRV} "grep -v '^$' /etc/selinux/config|grep -v '^#'" >> ${OUT}-${SRV}
echo "#####" >> ${OUT}-${SRV}
ssh ${USR}@${SRV} "sudo grep 'Failed password' /var/log/secure" >> ${OUT}-${SRV}
echo "#####" >> ${OUT}-${SRV}
doneExecute the /home/student/bin/bash-lab script, and review the output content on workstation.
On workstation, execute the /home/student/bin/bash-lab script.
[student@workstation ~]$ bash-labReview the content of the /home/student/output-servera and /home/student/output-serverb files.
[student@workstation ~]$cat /home/student/output-serveraservera.lab.example.com ##### CPU op-mode(s): 32-bit, 64-bit CPU(s): 2 CPU family: 6 ##### SELINUX=enforcing SELINUXTYPE=targeted ##### Apr 1 05:42:07 servera sshd[1275]: Failed password for invalid user operator1 from 172.25.250.9 port 42460 ssh2 Apr 1 05:42:09 servera sshd[1277]: Failed password for invalid user sysadmin1 from 172.25.250.9 port 42462 ssh2 Apr 1 05:42:11 servera sshd[1279]: Failed password for invalid user manager1 from 172.25.250.9 port 42464 ssh2 ##### [student@workstation ~]$cat /home/student/output-serverbserverb.lab.example.com ##### CPU op-mode(s): 32-bit, 64-bit CPU(s): 2 CPU family: 6 ##### SELINUX=enforcing SELINUXTYPE=targeted ##### Apr 1 05:42:14 serverb sshd[1252]: Failed password for invalid user operator1 from 172.25.250.9 port 53494 ssh2 Apr 1 05:42:17 serverb sshd[1257]: Failed password for invalid user sysadmin1 from 172.25.250.9 port 53496 ssh2 Apr 1 05:42:19 serverb sshd[1259]: Failed password for invalid user manager1 from 172.25.250.9 port 53498 ssh2 #####
This concludes the section.