1

I have a list of many servers. need to check (actually do some works on them) if the private key on the hand still can be used to login with SSH.

If I use ssh -i /path/to/private_key admin@server_host. it will just hang there asking for password: admin@server_host's password:

What can I do to avoid this kind of hang, and collect info when private key is not applicable for this server?

Zhenkai
  • 318
  • 3
  • 15
  • possible duplicate of [ssh script that automatically enters password](http://stackoverflow.com/questions/12202587/ssh-script-that-automatically-enters-password) – jww Apr 29 '14 at 03:25
  • nope, I don't have a password, all I have is a private RSA key. there is no need to use expect to automate "type" password in this case. – Zhenkai Apr 29 '14 at 03:29
  • you might want to add that to the question. As its phrased, it sounds like the program is hung waiting for a password and you're interested in entering a password to avoid the hang. – jww Apr 29 '14 at 03:31
  • actually, I found a duplicate of this question [here](http://stackoverflow.com/questions/21260695/bash-check-if-i-can-ssh-with-keys-to-a-list-of-hosts?rq=1). Answer in that question works better than answer I accepted here. – Zhenkai Apr 29 '14 at 05:00
  • There's more than one way to skin a cat, so its not surprising to find other useful ways to do what you want. You should upvote the fellow's question and the other fellow's answer since they helped you. That ensures quality questions and answers receive are easily found by others with similar questions. – jww Apr 29 '14 at 05:05

3 Answers3

2

You can tell ssh to not allow password authentication using:

ssh -i /path/to/private_key -oPasswordAuthentication=no admin@server_host

This will cause ssh to exit immediately if the private key does not work.

If you would like it to just exit if it is successful also, just add that command to the end:

ssh -i /path/to/private_key -oPasswordAuthentication=no admin@server_host exit
mbonneau
  • 627
  • 3
  • 8
1

I couldn't get the PasswordAuthentication=no option to work with Mac OS/X 10.9.2.

However, I was able to get the desired behavior with NumberOfPasswordPrompts=0.

Mark J. Bobak
  • 13,720
  • 6
  • 39
  • 67
1

Actually I find another more accurate option available in ssh which can be used to solve my problem.

-oPreferredAuthentications="publickey"

After add this option, ssh connection will only use authentication with the private key I provided instead of trying other methods first (and ruled out password like the answer I accepted). This even speed up whole process.

Zhenkai
  • 318
  • 3
  • 15