centos 集群自动免密登录脚本

步骤:

  • 修改以下两个文件中的节点名和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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值