I'm automating auth key uploads to several ssh hosts and I want to avoid adding the key to authorized_keys every time the script is executed, so I check if ssh can connect with key authentication before uploading the key.
Problem is that the script stops its loop on the first server the user already has a key in.
The script tries out key authentication based on advice from bash/ssh test for public key authentication
while read SERVER; do
CONN="$USER@$SERVER"
echo "$CONN: "
ssh -q -o "BatchMode yes" $CONN 'true'
RC=$?
if [[ $RC -ne 0 ]]
then
echo "key auth did not succeed, trying to uploading key:"
../ssh-uploadkeys/ssh-uploadkeys $CONN
else
echo "key auth ok, no need to upload key"
fi
done < servers.txt
This outputs:
[email protected]:
key auth ok, no need to upload key
The servers.txt:
the.host.com
another.host.com
the.ghost.com
The ssh-uploadkeys script that my script uses is written by Tero Karvinen: http://terokarvinen.com/ssh-uploadkeys.html