Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check is SSH prompt for password or not

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

like image 671
theuniverseisflat Avatar asked Feb 27 '14 22:02

theuniverseisflat


2 Answers

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
like image 156
nwk Avatar answered Sep 27 '22 15:09

nwk


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.

like image 30
Hermdog Avatar answered Sep 27 '22 15:09

Hermdog