Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to read my ssh warning banner via bash script

#!/bin/bash
if read -ra banner < <(LANG=C sshd -T | grep banner 2>/dev/null)
then
        printf "$banner"
else
        printf 'banner not set\n'
fi

How can i fix my bash script to read my ssh warning banner. new to bash scripting and isn’t sure what I am doing wrong

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

To get a good answer, it is better if you describe what output or behaviour you’re seeing, and what output you expected.

I see two things wrong with your snippet:

  • sshd -T prints banner none when the banner is not set, so your grep will still return a line, making the if pointless
  • You request banner to be an array with read -a, but then only access the first element when printing

You could fix your code like this

if read -ra banner < <(LANG=C sshd -T | grep banner | grep -v "^banner none$"); then
  printf "${banner[@]:1}"
else
  printf 'banner not set\n'
fi

I prefer a different style, maybe something like

shopt -s lastpipe
LANG=C sshd -T | grep ^banner\  | read -r banner
if test "$banner" != "banner none"; then
  echo "${banner#* }"
else
  echo "banner not set"
fi
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading