在脚本中使用expect命令实现ssh密钥的自动下发
准备需远程登录的地址
[root@use08 ~]# cat > /tmp/hosts.txt << EOF
> 192.168.247.15
> 192.168.247.16
> 192.168.247.17
> EOF
准备脚本
[root@use08 ~]# cat ssh_key.sh
#!/bin/bash
#1.password
password=mage
#2.keys
ssh-keygen -t rsa -P "" -f /root/.ssh/id_rsa &> /dev/null
#3.install expect
rpm -q expect &> /dev/null || yum install expect -y &> /dev/null
#4.send key
while read ip;do
expect << EOF
set timeout 20
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
expect eof
EOF
done < /tmp/hosts.txt