I'm trying to copy a bunch of RSA keys to multiple servers for a specific user.
Whenever I issue the ssh-copy-id command it asks me to confirm by typing "yes", then asks me for the password.
I wanted to avoid wearing out my arms and fingers, so, I decided to create a script for this task, something like this:
#!/bin/bash
runuser -u $RMTUSER -- ssh-copy-id [email protected]
runuser -u $RMTUSER -- ssh-copy-id [email protected]
(...)
runuser -u $RMTUSER -- ssh-copy-id [email protected]
runuser -u $RMTUSER -- ssh-copy-id [email protected]
I can't seem to find a good way to automate that task. Nothing seems to work. How can I input "yes" and the password automatically?
I've realized my initial question was quite lackluster. I'm sorry for that... it remains above, though.
I've improved the script to something similar to what Marcus proposed. I'm stuck at the "for" loop wondering how to pass that password for different server arrays.
My host sets are all static and there's much more.
#!/bin/bash
LOCUSER="$1" # USER FOR REMOTE ACCESS
RMTUSER="$2" # REMOTE USER
PASSWD="$3" # SITE PASSWORD
SITE="$4" # SERV SITE
function uras() {
for IP in "$@"; do
runuser -u "${LOCUSER}" -- sshpass "-p${PASSWD}" ssh-copy-id "${RMTUSER}@${IP}"
[ "$?" -eq "0" ] && echo "OK - $IP" || echo "FAIL! - $IP"
done
}
case $SITE in
"sa")
ARRAY_A=( $(cat ./serv_a.txt) )
uras "${ARRAY_A[@]}"
;;
"sb")
ARRAY_B=( $(cat ./serv_b.txt) )
uras "${ARRAY_B[@]}"
;;
"sc")
ARRAY_C=( $(cat ./serv_c.txt) )
uras "${ARRAY_C[@]}"
;;
*)
echo "INVALID SITE"
;;
esac
Still, the script fails for every host.
# ./auto_ssh_copy.sh [user] root [pass] [site]
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/var/lib/zabbix/.ssh/id_rsa.pub"
FAIL! - 172.24.168.48
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/var/lib/zabbix/.ssh/id_rsa.pub"
FAIL! - 172.24.168.49
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/var/lib/zabbix/.ssh/id_rsa.pub"
FAIL! - 172.24.168.50
(...)
I tried using "-f" as well, but the result is the same.
I agree that using something robust like Ansible might be a better tool for the job, but unfortunately it is just not available in my working set for now. This is what I came up with so far.
Finally I managed to copy all keys.
The script above was missing option -o StrictHostKeyChecking=no, thus sshpass was returning exit code 6.
The resulting command is this:
runuser -u ${LOCUSER} -- sshpass -v -p${PASSWD} ssh-copy-id -o StrictHostKeyChecking=no ${RMTUSER}@${IP}
Marcus awnser help a lot. Thanks everybody.