脚本内容如下:
#!/bin/bash
#-t指定SSH密钥的算法为RSA算法,-N设置密码为空,-f指定生成的密钥文件存放位置
rm -rf ~/.ssh/{known_hosts,id_rsa*}
ssh-keygen -t rsa -n '' -f ~/.ssh/id_rsa //生成密钥
yum -y install expect //安装expect预期交互
for i in {51..59}
do
expect << EOF
spawn ssh-copy-id 192.168.4.$i //使用for循环将公钥传给(51-59)其他主机
expect "(yes/no)?" {send "yes
"}
expect "password:" {send "123456
"}
expect "#" {send "exit
"}
EOF
done