yum install expect -y # 安装expect
[root@Toiny ~]# cat auto_ssh.sh
#!/bin/bash
rm -f /root/.ssh/*
# 删除原互信IP记录
passwd='123456'
# 定义passwd变量,要求密码一样
for ip in `cat ip.list`
# ip.list为互信ip列表
do
/usr/bin/expect << EOF
spawn ssh-keygen -t rsa -b 2048
expect {
"*id_rsa" {send "\r";exp_continue}
"*phrase" {send "\r";exp_continue}
"*again" {send "\r"}
}
spawn ssh-copy-id $ip
expect {
"*yes/no" {send "yes\r";exp_continue}
"*password:" {send "$passwd\r"}
}
expect eof
EOF
done
ip.list 存放节点ip信息,每个ip一行;
192.168.2.22
192.168.2.23
.......