安装依赖
sudo yum install expect
相关脚本
#!/bin/bash
if [ ! -f ~/.ssh/id_rsa ];then
ssh-keygen -t rsa
else
echo "id_rsa has created ..."
fi
while read line
do
ip=`echo $line | cut -d " " -f 1`
user="bigdata"
passwd="xxx"
expect <<EOF
set timeout 10
spawn ssh-copy-id -i /home/bigdata/.ssh/id_rsa.pub $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$passwd\n" }
}
expect "password" { send "$passwd\n" }
EOF
done < hostlist.txt
对应机器文件
vim hostlist.txt
bigdata1
bigdata2
bigdata3
bigdata4
bigdata5
bigdata10
bigdata11
bigdata12
bigdata13
bigdata14
评论区