准备存放ip的文件ip.txt
vim ip.txt
192.168.226.164
192.168.226.165
#!/usr/bin/bash
##ssh-key ssh-copy-id
##weqiang li
rpm -q expect &> /dev/null
if [ $? -ne 0 ] ;then
yum install -y expect &>/dev/null
fi
get_keygen(){
/usr/bin/expect <<-EOF &>/dev/null
spawn ssh-keygen
expect {
".ssh/id_rsa):" { send "\r"; exp_continue }
"Overwrite (y/n)?" { send "y\r"; exp_continue }
"no passphrase):" { send "\r"; exp_continue }
"passphrase again:" { send "\r" };
}
interact
expect eof
EOF
}
send_key(){
/usr/bin/expect <<-EOF &>/dev/null
#(呼唤expct 把我们想用expct的地方 用段落标记把他放进去)
spawn ssh-copy-id -i $ip
expect {
"connecting (yes/no)?" { send "yes\r"; exp_continue}
"password:" { send "1\r"}
}
interact
expect eof
#(说完了 关闭对话)
EOF
}
echo "正在制作密钥"
echo "存放的ip的文件是ip.txt,对方密码是必须是1"
sleep 5
if [ ! -f ~/.ssh/id_rsa ];then
get_keygen
fi
for ip in `cat ip.txt`
do
{
send_key $ip
if [ $? -eq 0 ] ;then
echo "$ip,密钥发送成功"
else
echo "$ip,密钥发送失败"
fi
}&
done
wait
echo "完成任务"