Reputation: 488
I'm trying to check all servers in an array to see whether they are configured with the SSH password-less trust or not.
If some are not, I'd like to print the server details, and keep looping through the array elements (ips) until all have been checked.
(I want to have the opportunity to print all the erroneous ones before exiting.)
I tried the following approach, but it exits the loop after it meets the first unconfigured server:
for svr in "${table[@]}"
do
SSH=$(ssh -o PasswordAuthentication=no -o StrictHostKeyChecking=no $svr echo)
if $(echo [ $? -ne 0 ])
then echo "Trust is not configured for ${table[$svr]}"
exit "$SOME"
else
:
fi
done
What am I doing wrong?
Upvotes: 1
Views: 1440
Reputation: 125778
Well, the immediate problem is that you have an exit
command in the middle of the loop... which will in fact exit the script when it hits that point. If you don't want it to exit until the loop has finished running, put the exit
command after the loop.
But you're also checking whether ssh
succeeded in a really weird way. Unless there's something I don't understand involved, just put the ssh
command directly in the if
condition, and discard its output with >/dev/null 2>&1
:
for svr in "${table[@]}"
do
if ssh -o PasswordAuthentication=no -o StrictHostKeyChecking=no $svr echo >/dev/null 2>&1
then
echo "Trust is not configured for $svr"
fi
done
exit "$SOME"
Note that I also fixed the reference to ${table[$svr]}
(which doesn't make sense), and removed the else
clause (which wasn't doing anything). Also, what's $SOME
?
EDIT: If you want it to exit if any of the server connections fail, you need to keep track of whether there's been a failure as the loop runs, then use that to control whether it exits at the end.
failures=0
for svr in "${table[@]}"
do
if ssh -o PasswordAuthentication=no -o StrictHostKeyChecking=no $svr echo >/dev/null 2>&1
then
echo "Trust is not configured for $svr"
((failures++))
fi
done
if ((failures>0))
then
exit
fi
Upvotes: 1