Home > database >  how to read my ssh warning banner via bash script
how to read my ssh warning banner via bash script

Time:01-23

#!/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

CodePudding user response:

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
  •  Tags:  
  • Related