check is SSH prompt for password or not

2019-04-07 00:16发布

问题:

Hi I have a list of few hundred hosts. I want to run a command using ssh in a loop, if my ssh keys are set properly, then I execute a command if I get challenge for password I want to skip to the next host

So lets say I have hosta and hostb and hostc. I can do a ssh to hosta & hostc , but hostb is challenging me for password. Is there a way to check if a hosts will challenge me for password or not? So my logic would be

if I get challenge from $host; then
   skip host 
else
   ssh $host 'command'
fi 

I hope this makes sense. Thanking you in advance

回答1:

for host in host1 host2 host3; do
    ssh -o PasswordAuthentication=no $host command
done

To make it parallel add &:

for host in host1 host2 host3; do
    ssh -o PasswordAuthentication=no $host command &
done


回答2:

If this is a thing you do regularly, I would suggest looking at dsh. http://www.tecmint.com/using-dsh-distributed-shell-to-run-linux-commands-across-multiple-machines/

it allows you to make a list of your servers, and run commands against ALL of them, or just subsets(web, db, app, etc)

you can create global files, or create your own personal files.



回答3:

ssh has an option, called BatchMode.

You can use it like

ssh -o BatchMode ...

and it won't ask you anything, but skip the connection attempt.



回答4:

Or use rundeck - this can be found at http://rundeck.org