Bookmark this page

Lab: Improve Command-line Productivity

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

  1. 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.

    1. On the workstation machine, create the /home/student/bin/ directory if needed.

      [student@workstation ~]$ mkdir -p /home/student/bin
    2. Use the vim command to create and edit the /home/student/bin/bash-lab script file.

      [student@workstation ~]$ vim ~/bin/bash-lab
    3. Insert the following text and save the file.

      #!/usr/bin/bash
    4. Make your script file executable.

      [student@workstation ~]$ chmod a+x ~/bin/bash-lab
  2. Edit 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 fileContent 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.

    Note

    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 (|).

    1. Use the vim command to open and edit the /home/student/bin/bash-lab script file.

      [student@workstation ~]$ vim ~/bin/bash-lab
    2. Append the following lines to the /home/student/bin/bash-lab script 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
  3. Execute the /home/student/bin/bash-lab script, and review the output content on workstation.

    1. On workstation, execute the /home/student/bin/bash-lab script.

      [student@workstation ~]$ bash-lab
    2. Review the content of the /home/student/output-servera and /home/student/output-serverb files.

      [student@workstation ~]$ cat /home/student/output-servera
      servera.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-serverb
      serverb.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
      #####

Evaluation

As the student user on the workstation machine, use the lab command to grade your work. Correct any reported failures and rerun the command until successful.

[student@workstation ~]$ lab grade console-review

Finish

On the workstation machine, change to the student user home directory and use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish console-review

Revision: rh134-9.3-5fd2368