说明::这个脚本主要用于安装ansible和同网段主机你要做免交互的自动做免交互
简化版::
首先需要写一个文件Ip_list.txt 里面放入你想做免交互的ip地址
echo -e -n "请输入\033[34mroot\033[0m用户密码 >>> "
read -p "" pwd
create_key(){
/usr/bin/expect <<EOF
# 设置捕获字符串后,期待回复的超时时间
#set timeout 10
# 服务器生成公钥
spawn ssh-keygen -t rsa
expect {
"Enter file" { send "\n"; exp_continue }
"Enter passphrase" { send "\n"; exp_continue }
"Enter same" { send "\n"}
}
expect eof
EOF
}
copy_key () {
for a in Ip_list.txt
do
/usr/bin/expect <<EOF
# 发送公钥给对方服务器
spawn ssh-copy-id root@$a
expect {
"yes/no" { send "yes\n"; exp_continue }
"password:" { send "${pwd}\n"}
}
expect eof
EOF
done
}
create_key
copy_key