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 @   基础狗  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示