最好用的信任关系自动化脚本(shell)

1,创建三个文件, 2 father.txt 放需要建立信任关系的服务器ip列表,格式如下 3 passwd.txt 为服务器的登录密码,要求所有服务器的密码一样,如果不一样可以修改rsa.sh脚本 4 rsa.sh 为建立信任关系的脚本

#!/bin/bash
#ssh-keygen -t  rsa

basepath=$(cd `dirname $0`; pwd)
echo $basepath
father_file_name=father.txt


#生成密钥
if [ ! -f ~/.ssh/id_rsa ] ; then
  ssh-keygen -t rsa -P  -f ~/.ssh/id_rsa
fi

cat ~/.ssh/id_rsa.pub >>~/.ssh/authorized_keys

pw=`cat $basepath/passwd.txt`
op_flag=$2

#更新各机器的hosts文件及公匙信息
for HOSTNAME in `cat ${basepath}/${father_file_name}| grep -v 127.0.0.1| grep -v ::1|awk {print $1}`
do
{
        sleep 2
        mycommand="ssh-copy-id -i /root/.ssh/id_rsa.pub root@${HOSTNAME}"
        expect -c "
        spawn ${mycommand};
        set timeout 60
        expect {
                 "password:"             {send "${pw}
";  exp_continue}
                 "connecting (yes/no)?"  {send "yes
";    exp_continue}
               }
        "

        sleep 2

}&
done
wait
经验分享 程序员 微信小程序 职场和发展