SSH免密配置脚本

此脚本用于批量设置免密登录使用。

  • 使用前请分别修改两个脚本中的机器名和密码
  • 将两个脚本文件复制到集群中的一台机器上
  • 需要保证两个脚本文件处于同一级目录下
  • 在集群中某一台机器上执行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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值