ssh免密配置种子脚本-自动认证

#清除ssh
cd  ~/.ssh;
rm -rf id_rsa;
rm -rf id_rsa.pub;
rm -rf authorized_keys;


#生成密钥
expect << ooff
set timeout 60
spawn ssh-keygen -t rsa
expect {
"Enter file in which to save the key" {send "\r";exp_continue}
"Enter passphrase (empty for no passphrase):" {send "\r";exp_continue}
"Enter same passphrase again: " {send "\r"}
}
expect eof
ooff
#免认证文件
touch authorized_keys
cat id_rsa.pub >> authorized_keys
#分发
expect << ooff
set timeout 60
spawn ssh root@slaver1 
expect "password" {send "root\r"}
expect "~]#" {send "rm -rf ~/.ssh\r"}
expect "~]#" {send "mkdir ~/.ssh\r"}
expect "~]#" {send "exit\r"}
expect eof
ooff
expect <<ooff
set timeout 60
spawn scp /root/.ssh/authorized_keys root@slaver1:/root/.ssh/authorized_keys
expect "password" {send "root\r"}
expect eof
ooff
expect << ooff
set timeout 60
spawn ssh root@slaver2 
expect "password" {send "root\r"}
expect "~]#" {send "rm -rf ~/.ssh\r"}
expect "~]#" {send "mkdir ~/.ssh\r"}
expect "~]#" {send "exit\r"}
expect eof
ooff
expect <<ooff
set timeout 60
spawn scp /root/.ssh/authorized_keys root@slaver2:/root/.ssh/authorized_keys
expect "password" {send "root\r"}
expect eof
ooff





#制作免认证文件
expect <<ooff
spawn ssh root@slaver1
expect "~]#" {send "ssh-keygen -t rsa \r"}
expect {
"Enter file in which to save the key" {send "\r"; exp_continue}
"Enter passphrase (empty for no passphrase)" {send "\r"; exp_continue}
"Enter same passphrase again" {send "\r"}
}

expect "~]#" {send "cd /root/.ssh\r"}
expect ".ssh]#" {send "scp /root/.ssh/id_rsa.pub root@master:/root/.ssh/id_rsa.pub.slaver1 \r"}
expect "can't be established" {send "yes\r"}
expect "password" {send "root\r"}
expect ".ssh]#" {send "exit\r"}
expect eof
ooff
expect <<ooff
spawn ssh root@slaver2
expect "~]#" {send "ssh-keygen -t rsa \r"}
expect {
"Enter file in which to save the key" {send "\r"; exp_continue}
"Enter passphrase (empty for no passphrase)" {send "\r"; exp_continue}
"Enter same passphrase again" {send "\r"}
}

expect "~]#" {send "cd /root/.ssh\r"}
expect ".ssh]#" {send "scp /root/.ssh/id_rsa.pub root@master:/root/.ssh/id_rsa.pub.slaver2 \r"}
expect "can't be established" {send "yes\r"}
expect "password" {send "root\r"}
expect ".ssh]#" {send "exit\r"}
expect eof
ooff

#分发
cd /root/.ssh/;
cat id_rsa.pub.slaver1 >> authorized_keys
cat id_rsa.pub.slaver2 >> authorized_keys
scp /root/.ssh/authorized_keys root@slaver1:/root/.ssh/authorized_keys
scp /root/.ssh/authorized_keys root@slaver2:/root/.ssh/authorized_keys



















posted @ 2024-11-22 14:39  基础狗  阅读(5)  评论(0编辑  收藏  举报