Red Hat System Administration II
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
grepand 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-labscript file on theworkstationmachine. The initial content in the script must use the shebang interpreter directive.On the
workstationmachine, create the/home/student/bin/directory if needed.[student@workstation ~]$
mkdir -p /home/student/binUse the
vimcommand to create and edit the/home/student/bin/bash-labscript 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-lab
Edit your newly created script file to store the following information from the
serveraandserverbmachines on theworkstationmachine. 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-serveraand/home/student/output-serverbfiles respectively on theworkstationmachine. Use the hash sign (#) for differentiating the output of the successive commands in the output file.Command or file Content requested hostname -fStore the entire output. echo "#####"Append the hash signs to differentiate the following command. lscpuGet only the lines that start with the CPUstring.echo "#####"Append the hash signs to differentiate the following command. /etc/selinux/configIgnore empty lines. Also, ignore lines that start with the #character.echo "#####"Append the hash signs to differentiate the following command. /var/log/secureGet all "Failed password" entries. echo "#####"Append the hash signs to differentiate the following command. Save the required information to the
output-serveraandoutput-serverbfiles in the/home/studentdirectory onworkstation.Note
You can use the
sudocommand without requiring a password on theserveraandserverbhosts. Remember to use a loop to simplify your script. You can also use multiplegrepcommands that are concatenated with the use of the pipe character (|).Use the
vimcommand to open and edit the/home/student/bin/bash-labscript file.[student@workstation ~]$
vim ~/bin/bash-labAppend the following lines to the
/home/student/bin/bash-labscript file. The number of hash signs is arbitrary.Note
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} done
Execute the
/home/student/bin/bash-labscript, and review the output content onworkstation.On
workstation, execute the/home/student/bin/bash-labscript.[student@workstation ~]$
bash-labReview the content of the
/home/student/output-serveraand/home/student/output-serverbfiles.[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 #####