步骤:
- 修改以下两个文件中的节点名和root密码,
- 在主节点运行ssh_non_pwd_login.sh
ssh_non_pwd_login.sh
# !/bin/bash
SERVERS="master node1 node2"
PASSWORD=123456
for SERVER in $SERVERS
do
yum install -y expect
# 批量创建文件夹
echo 'no this dir and then will create it.'
expect -c "set timeout -1;
spawn ssh root@$SERVER mkdir -p /root/bin/ssh
expect {
*password:* {send -- $PASSWORD\r;exp_continue;}
*(yes/no)* {send -- yes\r;exp_continue;}
eof {exit 0;}
}"
expect -c "set timeout -1;
spawn scp ./ssh_non_pwd_login.sh $SERVER:/root/bin/ssh
expect {
*password:* {send -- $PASSWORD\r;exp_continue;}
*(yes/no)* {send -- yes\r;exp_continue;}
eof {exit 0;}
}"
expect -c "set timeout -1;
spawn ssh root@$SERVER bash /root/bin/ssh/ssh_non_pwd_login.sh
expect {
*password:* {send -- $PASSWORD\r;exp_continue;}
*(yes/no)* {send -- yes\r;exp_continue;}
eof {exit 0;}
}"
done
ssh_non_pwd_login.sh
# !/bin/bash
SERVERS="master node1 node2"
PASSWORD=123456
auto_gen_ssh_key() {
expect -c "set timeout -1;
spawn ssh-keygen;
expect {
*(/root/.ssh/id_rsa)* {send -- \r;exp_continue;}
*passphrase)* {send -- \r;exp_continue;}
*again* {send -- \r;exp_continue;}
*(y/n)* {send -- y\r;exp_continue;}
*password:* {send -- $PASSWORD\r;exp_continue;}
eof {exit 0;}
}";
}
auto_ssh_copy_id() {
expect -c "set timeout -1;
spawn ssh-copy-id $1;
expect {
*(yes/no)* {send -- yes\r;exp_continue;}
*password:* {send -- $2\r;exp_continue;}
eof {exit 0;}
}";
}
auto_copy_id_to_all() {
for SERVER in $SERVERS
do
auto_ssh_copy_id $SERVER $PASSWORD
done
}
yum install -y expect
auto_gen_ssh_key
auto_copy_id_to_all