准备搭建一个集群,但是第一步需要设置各个机器的免密登陆,我现在有十台机器,如果挨个设置的话工作量太大,所以决定写一个脚本,这个脚本需要做的功能有
- 给每个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
此脚本可以使一台机器和其他机器实现免密,每台机器执行一次即可