check is SSH prompt for password or not

限于喜欢 提交于 2019-12-09 17:23:46

问题


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



来源:https://stackoverflow.com/questions/22081581/check-is-ssh-prompt-for-password-or-not

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!