0

I am checking ssh connection through bash script

echo -e "Checking if you have setup a key for serve ssh connection ..."
timeout 5s ssh uname@host 'exit'
if [ $? -eq 0 ];
then
  echo -e "You're done"
else
  echo -e "Set up a ssh key please or check username password"
fi

Script is working fine. It prompts for password.

But the problem is that it is asking password without any message, not showing any prompt message like:

uname@host's Password:

I want this message to be displayed.

0

1 Answer 1

2

your way of checking whether an SSH key is setup is very bad.

  1. It can fail (e.g, when the connection takes more than 5s to establish, or when the shell of the user doesn't understand "exit", or when anything else doesn't happen within 5s) even if the user has a SSH key set up,
  2. and it can succeed even when the user hasn't (when the connection gets established in less than 5s and the user enters a password quick enough, which is quite commonly the case!), or if there's already a connection established,
  3. and it potentially leaves half-finished SSH connections lying around.

You need to replace that method. It simply doesn't do it's job.

That's rather easy, tell SSH to not allow password authentication; depending on your version of SSH, there's an option for that (man ssh); most likely a

ssh -n -oControlMaster=no \
       -oPasswordAuthentication=no \
       -oKbdInteractiveAuthentication=no \
       -oPreferredAuthentications=publickey \
       user@host true \
|| \
echo "can't log in"

does what you want, without any of the bad ideas involving timeouts and so on. Thing works, or it fails.

Problem: it will print the same, no matter whether you couldn't connect to the host, or whether you could not authenticate. Those are different things.


by the way, if the whole thing is just meant to enable users to make sure they've got publickey auth set up: There's the ssh-copy-id user@host, which tries to log in (how ever possible), and then checks whether the user's public key is installed on the server. That would probably obsolete your whole script :)

2
  • Thans very much for your answer. Commented Apr 12, 2022 at 3:53
  • 1
    Thank you very much for your answer. This is just a small and simplified part of script. My scenario is I dont know whether there is password less ssh connection or not, and May be there is ssh without key or in many case password is mandatory. I just want that it should prompt the message to ask for password. running this command in terminal shows the message but while running it from script it doesn't. Commented Apr 12, 2022 at 3:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .