码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2019-09-10 01:19:39      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:shell脚本   服务   服务器   for   choice   配置   fine   exec   sts   

#!/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

#自己的微信群链接
https://blog.51cto.com/amunlinux/2426485

技术图片

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

标签:shell脚本   服务   服务器   for   choice   配置   fine   exec   sts   

原文地址:https://blog.51cto.com/amunlinux/2436914

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!