shell script deploy passwordless access

In order to realize the automatic deployment of some components, it may be convenient to set the password-free access of the server. Finally, the automatic deployment password script is realized. The following is the implementation code logic. The code is relatively large and cannot be written out one by one.
#!/bin/bash

ips=( '10.100.112.79' '10.100.112.203' '10.100.112.204' '10.100.112.205' '10.100.112.95' '10.100.112.125' ) //all associated ip
masterLenth=4 // The first few ips sets need to be configured with unsecured access
gname=hgroup
uname=root //unsecured access account (root unsecured access rights must be generated first)
password="123456" //unsecured access password
currpath=$PWD

hostip=` /sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:" `//Get the local ip,
echo "host_ip$hostip "
#If it is root permission, Mr. free key from root
if [ "$uname" = "root" ];then
ssh root@${ips[i]} yum install -y expect
expect <<



send "$password\n";
exp_continue
}
"*id_rsa):" {
send "\n";
exp_continue
}
"*(y/n)?" {
send "y\n"
exp_continue
}
"*passphrase):" {
send "\n"
exp_continue
}
"*again:" {
send "\n"
}
}
expect eof
EOF
#initSshdConfig initializes the sshd-config file for secret-free access, the initSshdConfig code links this blog sshd_config secret-free access processing
sh $currpath/ initSshdConfig. sh $hostip $currpath $hostip
fi #Add

accounts to all machines
for ip in ${ips[@]}
do #If
it is a root account, first authorize root to access other machines without password
if [ "$uname" = "root" ];then
sh $currpath/resrsa.sh $ip $uname $password
echo "$hostip------------------>$ip"
fi

if [ "$ip" = "$hostip" ];then
echo  "$hostip"
   sh $currpath/useradd.sh $gname $uname $password
#    rm -rf /usr/$uname/.ssh
#   sh apecttest.sh $gname $uname $name $ip

else
#   echo ${ip}
#   ssh ${ip} mkdir -p $currpath -m 777
#   scp -r $currpath/base root@${ip}:$currpath/
#   scp $currpath/useradd.sh root@${ip}:$currpath/
   if [  "$uname" -ne "root" ];then
#新增账号 http://lyongq04.iteye.com/blog/2297255
   ssh ${ip} $currpath/useradd.sh $gname $uname $password
   fi
fi
done

#Give the ips former masterLenth server permission to access all machines without password, such machines are temporarily referred to as master
for((i=0;i<masterLenth;i++))
do
echo "master ${ips[i]}"
# scp $currpath/initSshdConfig.sh root@${ips[i]}:$currpath/
sh $currpath/initSshdConfig.sh ${ips[i]} $currpath $hostip
if [ "${ips[i]}" ! = "$hostip" ];then
# scp /etc/ssh/sshd_config root@${ips[i]}:/etc/ssh
scp $currpath/resrsa.sh root@${ips[i]}:$currpath/
fi

#Remotely generate keys for the master account# if
expect not install
ssh root@${ips[i]} yum install -y expect
if [[ "$uname" != "root" || "$uname" == " root" && "${ips[i]}" != "$hostip" ]];then
echo "$hostip ssh-keygen-------------------------------------------------------->>${ips[i]}"
expect <<EOF
spawn ssh $uname@${ips[i]} ssh-keygen -t rsa
expect {
"*password:"    {
send "$password\n";
exp_continue
}
"*id_rsa):"    {
send "\n";
exp_continue
}
"*(y/n)?" {
send "y\n"
exp_continue
}
"*passphrase):" {
send "\n"
exp_continue
}
"*again:" {
send "\n" all masters to access all machines without password done #Authorize fi EOF expect eof }
}






for ip in ${ips[@]}
do
for((i=0;i<masterLenth;i++))
do
if [ "${ips[i]}" = "$hostip" ];then
sh $currpath/resrsa.sh $ip $uname $password
else
ssh root@${ips[i]} "$currpath/resrsa.sh $ip $uname $password"
fi
done
done

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326625473&siteId=291194637