How to quickly check if a private rsa key still works for a bunch of servers in bash? [closed]

StackOverflow https://stackoverflow.com/questions/23354821

  •  11-07-2023
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top