Performance Checklist
In this lab, you will create a Bash script that can filter and get relevant information from different hosts.
Outcomes
You should be able to:
Create a Bash script and redirect its output to a file.
Use loops to simplify your code.
Filter the relevant content using grep and regular expressions.
Log in to workstation as student using student as the password.
On workstation, run the lab console-review start command.
This command runs a start script that determines if the workstation, servera and serverb machines are reachable on the network.
The script will alert you if they are not available.
It also installs the vim-enhanced and util-linux packages if needed, configures sudo, and prepares the content of /var/log/secure on servera and serverb.
[student@workstation ~]$lab console-review start
Create the /home/student/bin/bash-lab script file on workstation.
On workstation, create the /home/student/bin/ folder if needed.
[student@workstation ~]$mkdir -p /home/student/bin
Use vim to create and edit the /home/student/bin/bash-lab script file.
[student@workstation ~]$vim ~/bin/bash-lab
Insert the following text and save the file.
#!/bin/bash
Make your script file executable.
[student@workstation ~]$chmod a+x ~/bin/bash-lab
Edit your newly created script file to comply with the following requested information from the servera and serverb hosts.
The systems are configured to use SSH keys for authentication; a password is not required.
| Command or file | Content requested |
|---|---|
| hostname -f | Get all the output. |
| echo "#####" | Get all the output. |
| lscpu |
Get only the lines that start with the string CPU.
|
| echo "#####" | Get all the output. |
/etc/selinux/config
| Ignore empty lines. Ignore lines starting with #. |
| echo "#####" | Get all the output. |
/var/log/secure
| Get all "Failed password" entries. |
| echo "#####" | Get all the output. |
Save the required information to the new files /home/student/output-servera and /home/student/output-serverb.
You can use sudo 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 concatenated with the use of the pipe character (|).
Use vim to open and edit the /home/student/bin/bash-lab script file.
[student@workstation ~]$vim ~/bin/bash-lab
Append the following lines in bold to the /home/student/bin/bash-lab script file.
The following is an example of how you can achieve the requested script. In Bash scripting, you can take different approaches and obtain the same result.
#!/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}
done
Execute 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-lab
Review the content of /home/student/output-servera and /home/student/output-serverb.
[student@workstation ~]$cat /home/student/output-serveraservera.lab.example.com ##### CPU op-mode(s): 32-bit, 64-bit CPU(s): 2 CPU family: 21 CPU MHz: 2294.670 ##### SELINUX=enforcing SELINUXTYPE=targeted ##### Mar 21 22:30:28 servera sshd[3939]: Failed password for invalid user operator1 from 172.25.250.9 port 58382 ssh2 Mar 21 22:30:31 servera sshd[3951]: Failed password for invalid user sysadmin1 from 172.25.250.9 port 58384 ssh2 Mar 21 22:30:34 servera sshd[3953]: Failed password for invalid user manager1 from 172.25.250.9 port 58386 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 CPU MHz: 2294.664 ##### SELINUX=enforcing SELINUXTYPE=targeted ##### Mar 21 22:30:37 serverb sshd[3883]: Failed password for invalid user operator1 from 172.25.250.9 port 39008 ssh2 Mar 21 22:30:39 serverb sshd[3891]: Failed password for invalid user sysadmin1 from 172.25.250.9 port 39010 ssh2 Mar 21 22:30:43 serverb sshd[3893]: Failed password for invalid user manager1 from 172.25.250.9 port 39012 ssh2 #####