Files
be.ems/misc/cpsshkey.sh
2024-10-22 15:27:21 +08:00

37 lines
843 B
Bash
Executable File

#!/bin/bash
# if exist id_rsa
if [ ! -f ~/.ssh/id_rsa ];then
ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
else
echo "id_rsa has created ..."
fi
# distribute to hosts in file nehosts
while read line
do
# ignore comment or null line
if [[ "$line" =~ ^[^[:space:]]*# || -z "$line" ]]; then
continue
fi
ip=`echo $line | cut -d " " -f 1`
user=`echo $line | cut -d " " -f 2`
passwd=`echo $line | cut -d " " -f 3`
# !!! to remove ~/.ssh, all old authorized key will be lost
expect <<EOF
set timeout 10
spawn ssh $user@$ip "sudo rm -r ~/.ssh"
expect {
"yes/no" { send "yes\n";exp_continue }
"password:" { send "$passwd\n" }
}
spawn ssh-copy-id -f $user@$ip
expect {
"password:" { send "$passwd\n" }
}
expect eof
EOF
done < nehosts