此脚本用于批量设置免密登录使用。
- 使用前请分别修改两个脚本中的机器名和密码
- 将两个脚本文件复制到集群中的一台机器上
- 需要保证两个脚本文件处于同一级目录下
- 在集群中某一台机器上执行bach_ssh_non_pwd_login.sh脚本即可
batch_ssh_non_pwd_login.sh
# !/bin/bash
SERVERS="ceph8019 ceph8020 ceph8021 ceph8022"
PASSWORD=123456
for SERVER in $SERVERS
do
sudo yum -y install expect
# 批量创建文件夹
echo 'no this dir and then will create it.'
expect -c "set timeout -1;
spawn ssh ceph@$SERVER mkdir -p /home/ceph/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:/home/ceph/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 ceph@$SERVER /home/ceph/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="ceph8019 ceph8020 ceph8021 ceph8022"
PASSWORD=123456
auto_gen_ssh_key() {
expect -c "set timeout -1;
spawn ssh-keygen;
expect {
*(/home/ceph/.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 -y install expect
auto_gen_ssh_key
auto_copy_id_to_all