#!/bin/bash
clear
USER=root
PASSWD=amunlinux
PUBDIR=/root/.ssh
cat > ~/prompt-ans.txt << EOF
如果需要配置服务器数量过多,最好在外部保存到ip-list文件内,易于美观
如果.ssh/ 下存在文件,需要删除后再次执行脚本,不想删除注释关键字即可
服务器密码需要统一
EOF
cat ~/prompt-ans.txt
sleep 4s
yum -y install ansible expect*
which expect
如果外部已编辑此文件,需要注释此处
cat > ~/ip-list <<-EOF
10.20.30.4
10.20.30.5
EOF
if [ -f "$PUBDIR"/id_rsa ] || [ -f "$PUBDIR"/id_rsa.pub ]
then
echo -e "\033[33mPrompt: $PUBDIR/ File exists \033[0m"
ls -l "$PUBDIR" --color=auto
read -n1 -p "File exists,Want to delete ? [Y/y/N/n]" keyfile
case $keyfile in
Y | y)
echo " Fine,deleted,Execute the script again ‘$0‘" ;
sudo rm -fr "$PUBDIR"/id_rsa ;;
N | n)
echo " ok,good bye";
break ;;
)
echo " error choice";
break ;;
esac
else
/usr/bin/expect <<-EOF
spawn ssh-keygen -t rsa
expect "\/root\/.ssh\/id_rsa"
send "\r"
expect "empty for no passphrase"
send "\r"
expect "same passphrase again"
send "\r"
expect eof
EOF
for IP in $(cat ~/ip-list)
do
/usr/bin/expect <<-EOF
set timeout 5
spawn ssh-copy-id -i $PUBDIR/id_rsa.pub [email protected]$IP
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "$PASSWD\r" }
}
expect eof
EOF
done
wait
echo "------------finish-ok-------------"
rm -fr ~/prompt-ans.txt
fi
#自己的微信群链接
https://blog.51cto.com/amunlinux/2426485
原文地址:https://blog.51cto.com/amunlinux/2436913