Skip to content
Advertisement

Bash Scripting: How to display output for checking root logins via SSH not allowed?

My question is, how do i make the script into a if else statement that it checks if “PermitRootLogin no”, it should display the “Vulnerability: No”

and if the “PermitRootLogin yes” it should display as “Vulnerability: Yes”?

Thanks in advance!

Output

Script

#!/binbash
      echo "Audit criteria: Root login via SSH are not allowed"
      echo "Vulnerability: Yes"
      echo "Details: See below"
      echo
      echo "Command:"
      echo "grep "^PermitRootLogin" /etc/ssh/sshd_config"
      echo
      echo "Output:"
      grep "^PermitRootLogin" /etc/ssh/sshd_config

Advertisement

Answer

you can use variables to store the command output.

This script should do what you need-

#!/binbash
      permitrootlogin=`grep "^PermitRootLogin" /etc/ssh/sshd_config`
      if [[ $permitrootlogin == "PermitRootLogin no" ]]
      then 
      isVulnerability="No"
      else
      isVulnerability="Yes"
      fi
      echo "Audit criteria: Root login via SSH are not allowed"
      echo "Vulnerability: $isVulnerability"
      echo "Details: See below"
      echo
      echo "Command:"
      echo "grep "^PermitRootLogin" /etc/ssh/sshd_config"
      echo
      echo "Output:"
      echo $permitrootlogin
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement