批量设置免密登陆

准备搭建一个集群,但是第一步需要设置各个机器的免密登陆,我现在有十台机器,如果挨个设置的话工作量太大,所以决定写一个脚本,这个脚本需要做的功能有

  • 给每个ip命名
  • 生成密钥
  • 将密钥发送给另外一台机器
    其中需要安装一下except
    脚本内容为:
#!/bin/bash
yum -y install expect
cat << EOF > ~/.ssh/config
Host node01
HostName 36.*
User root
Port 10*


Host node02
HostName 36.*
User root
Port 10*

Host node03
HostName 36.*
User root
Port 10*

Host node04
HostName 36.*
User root
Port 10*

Host node05
HostName 36.*
User root
Port 10*

Host node06
HostName 36*
User root
Port 10*

Host node07
HostName 36.*
User root
Port 10*

Host node08
HostName 36.*
User root
Port 10*

Host node09
HostName 36*
User root
Port 10*

Host node10
HostName 36.*
User root
Port 10*
EOF
cat << EOF > ssh_expect.expect
#!/usr/bin/expect
spawn ssh-keygen -t rsa
expect {
     "save" { send "\n";exp_continue }
     "y/n" { send "y\n";exp_continue }
     "Enter passphrase" { send "\n";exp_continue }
     "Enter same" { send "\n";}
}

spawn ssh-copy-id -i node01
expect {
     "yes/no" { send "yes\n";exp_continue }
     "password" { send "****\n" }
}
spawn ssh-copy-id -i node02
expect {
     "yes/no" { send "yes\n";exp_continue }
     "password" { send "****\n" }
}
spawn ssh-copy-id -i node03
expect {
     "yes/no" { send "yes\n";exp_continue }
     "password" { send "****\n" }
}
spawn ssh-copy-id -i node04
expect {
     "yes/no" { send "yes\n";exp_continue }
     "password" { send "****\n" }
}
spawn ssh-copy-id -i node05
expect {
     "yes/no" { send "yes\n";exp_continue }
     "password" { send "****\n" }
}
spawn ssh-copy-id -i node06
expect {
     "yes/no" { send "yes\n";exp_continue }
     "password" { send "****\n" }
}
spawn ssh-copy-id -i node07
expect {
     "yes/no" { send "yes\n";exp_continue }
     "password" { send "****\n" }
}
spawn ssh-copy-id -i node08
expect {
     "yes/no" { send "yes\n";exp_continue }
     "password" { send "****\n" }
}
spawn ssh-copy-id -i node09
expect {
     "yes/no" { send "yes\n";exp_continue }
     "password" { send "****\n" }
}
spawn ssh-copy-id -i node10
expect {
     "yes/no" { send "yes\n";exp_continue }
     "password" { send "****\n" }
}
expect eof
EOF
chmod 777 ssh_expect.expect
./ssh_expect.expect

此脚本可以使一台机器和其他机器实现免密,每台机器执行一次即可

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值