ansible自动安装并配置ssh免密环境shell脚本

#!/bin/bash

创新互联拥有一支富有激情的企业网站制作团队,在互联网网站建设行业深耕十载,专业且经验丰富。十载网站优化营销经验,我们已为成百上千家中小企业提供了网站建设、成都网站建设解决方案,定制制作,设计满意,售后服务无忧。所有客户皆提供一年免费网站维护!

####### centos 7.6

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 $USER@$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




文章名称:ansible自动安装并配置ssh免密环境shell脚本
路径分享:http://azwzsj.com/article/ijhpgj.html