5.1.1-免密登录
优质
小牛编辑
135浏览
2023-12-01
1.1 入口
#!/bin/bash
masterip=`head -n 1 $1 |cut -d ' ' -f 1`
masterpasswd=`head -n 1 $1 |cut -d ' ' -f 2`
./clean_master.sh
./keygen_master.sh
last=`cat $1|grep ^1|wc -l`
for((i=2;i<=$last;i++))
do
slaveip=`head -n $i $1 |tail -n 1|cut -d ' ' -f 1`
slavepasswd=`head -n $i $1 |tail -n 1|cut -d ' ' -f 2`
./clean_slave.sh $slaveip $slavepasswd
./scp.sh $slaveip $slavepasswd
./signforkeygen.sh $slaveip $slavepasswd
./signforcpid.sh $slaveip $slavepasswd $masterip $masterpasswd
done
for((i=2;i<=$last;i++))
do
slaveip=`head -n $i a.txt |tail -n 1|cut -d ' ' -f 1`
slavepasswd=`head -n $i a.txt |tail -n 1|cut -d ' ' -f 2`
./scpid.sh $slaveip $slavepasswd
done
1.2 清理 master 节点
rm -rf /root/.ssh
1.3 清理 slave 节点
#!/usr/bin/expect
set ip [lindex $argv 0]
set password [lindex $argv 1]
spawn ssh $ip "rm -rf /root/.ssh/ /root/cpid.sh /root/keygen_slave.sh"
expect {
"(yes/no)?" { send "yes\r"; exp_continue }
"password:" { send "$password\r" }
}
interact
1.4 向 slave 节点传脚本
#!/usr/bin/expect
set ip [lindex $argv 0]
set password [lindex $argv 1]
spawn scp /root/keygen_slave.sh /root/cpid.sh $ip:`pwd`
expect {
"(yes/no)?" { send "yes\r"; exp_continue }
"password:" { send "$password\r" }
}
interact
1.5 master 节点生成 key
#!/bin/bash
ssh-keygen -t rsa -P '' -f /root/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
1.6 登录 slave 节点,调用生成 key 脚本
#!/usr/bin/expect
set ip [lindex $argv 0]
set password [lindex $argv 1]
spawn ssh $ip "./keygen_slave.sh"
expect {
"(yes/no)?" { send "yes\r"; exp_continue }
"password:" { send "$password\r" }
}
interact
1.7 slave 节点生成 key
ssh-keygen -t rsa -P '' -f /root/.ssh/id_rsa
1.8 登录 slave 节点,调用传输 key 脚本
#!/usr/bin/expect
set ip [lindex $argv 0]
set password [lindex $argv 1]
set desip [lindex $argv 2]
set despassword [lindex $argv 3]
spawn ssh $ip "./cpid.sh $desip $despassword"
expect {
"(yes/no)?" { send "yes\r"; exp_continue }
"password:" { send "$password\r" }
}
interact
1.9 slave 节点向 master 节点传输 key
#!/usr/bin/expect
set ip [lindex $argv 0]
set password [lindex $argv 1]
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@$ip
expect {
"(yes/no)?" { send "yes\r"; exp_continue }
"password:" { send "$password\r" }
}
interact
1.10 master 节点向 slave 节点传输 key
#!/usr/bin/expect
set ip [lindex $argv 0]
set password [lindex $argv 1]
spawn scp -rq /root/.ssh/authorized_keys root@$ip:/root/.ssh
expect {
"(yes/no)?" { send "yes\r"; exp_continue }
"password:" { send "$password\r" }
}
interact
1.11 配置文件 a.txt
172.18.1.5 123456
172.18.1.6 123456
注:配置文件无空行,无注释 。