Please help me out from below situation.

There are 1000 linux servers with me. i need to find out which all servers are accepting passwordless connection from my master server. Below is the command i used. problem with below command is, when the destination server ask for password then my loop stop working.

for i in `cat srv`; do echo Server $i\n;  ssh $i "uptime"; done


Server server001

04:56:01 up 62 days,  9:18,  0 users,  load average: 0.09, 0.14, 0.13

Server server002


How can i resolve it.?

  • if you have that many servers, you should be using pdsh (parallel distributed shell) or clusterssh or similar. here's an intro article about them: 4 Useful Tools to Run Commands on Multiple Linux Servers – cas Sep 17 '19 at 1:43
  • At 1000 servers you're well past the point where you should have set up something like ansible to manage the inventory a long time ago... – Shadur Jan 6 at 9:42

try -o BatchMode=yes as ssh option.

This prevent password conformation in command line, as well as accepting remote host fingerprint.

You may wish to check return code from ssh

for host in $(< serverlist) 
  echo testing $host
  if ssh -o BatchMode=yes $host uptime
  then echo host $host OK
  else echo host $host KO
done > result.log

With GNU Parallel you could do:

To find servers that do not accept passwordless login:

cat srv |
  parallel timeout 3 ssh {} true ' &>/dev/null ||' echo {}

To find servers that do accept passwordless login:

parallel --nonall --timeout 3 --slf srv --tag echo

Add -j100 to run on 100 servers at a time.


Use SSH options to specify you only want to use PubKey and not password authentication:

for i in `cat srv`; do echo Server $i\n;  ssh -o PubKeyAuthentication=yes -o PasswordAuthentication=no $i "uptime"; done

This is outside the scope of this question, but you might want to check the return value of the ssh command to determine wether the connection succeeded or not.

  • Thank you panki. Suggested changes in the loop stops at the same place where destination server ask for the password, Do i need to add any wait command to skip the loop if it waits more than 5 seconds. Will that help ? – Arun Sep 16 '19 at 9:19
  • What do you mean? The remote server should not be asking for your password, but instead return Permission denied (publickey,password). – Panki Sep 16 '19 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.