上百台linux的服务器互信,批量自动建立linux服务器之间的ssh互信
介紹:我們在部署無密碼訪問時,如果手工輸入.很費時間.下面方法用于自動化生成authorized_keys,免去了手工數據.
方法: 利用expect編寫sshkey.exp在遠程主機上生成id_rsa,并重定向到本地.在利用noscp.exp.把文件復制到遠程主機
步驟:(本方法在centos6.4 上驗證通過)
1 本地執行
ssh-keygen -t rsa
2 編寫腳本生成 authorized_keys (比如現在有cloud1 和 cloud2 服務器)
./sshkey.exp? cloud1 root root123 | grep ssh-rsa >> ~/.ssh/authorized_keys
./sshkey.exp? cloud2 root root123 | grep ssh-rsa >> ~/.ssh/authorized_keys
3 對于需要建立免密碼輸入的,把文件拷貝上去
./noscp.exp ~/.ssh/authorized_keys cloud1:~/.ssh root root123
./noscp.exp ~/.ssh/authorized_keys cloud2:~/.ssh root root123
4. 腳本說明
./sshkey.exp? 主機名 用戶名? 密碼??(在遠程主機生成id_rsa)
./noscp.exp?? 本地文件 遠程路徑? 遠程用戶密碼?? (無密碼拷貝文件)
注意:
每個主機上必須安裝 openssh-clients
#!/usr/bin/expect
#sshkey.exp
if {$argc<3} {
puts stderr "Usage: $argv0 host? user? passwd "
exit 1
}
set host [ lindex $argv 0 ]
set user? [ lindex $argv 1 ]
set pwd? [ lindex $argv 2 ]
set timeout 30
#spawn ssh? ${user}@${host} "rm -rf ~/.ssh/id_rsa*"
#
#expect {
# "*yes/no"?{ send "yes\r"; exp_continue }
# "*password:"?{ send "$pwd\r"; exp_continue? }
#}
spawn ssh? ${user}@${host} "ssh-keygen -t rsa"
expect {
"*yes/no"?{ send "yes\r"; exp_continue }
"*password:"?{ send "$pwd\r"; exp_continue? }
"Enter file in which to save the key*"?{ send "\n\r"; exp_continue }
"Overwrite*" { send "y\n"; exp_continue }
"Enter passphrase (empty for no passphrase):"?{ send "\n\r"; exp_continue }
"Enter same passphrase again:"?{ send "\n\r" }
}
spawn ssh? ${user}@${host} "cat ~/.ssh/id_rsa.pub"
expect {
"*yes/no"?{ send "yes\r"; exp_continue }
"*password:"?{ send "$pwd\r"? }
}
expect eof
#!/usr/bin/expect
#noscp.exp
if {$argc<4} {
puts stderr "Usage: $argv0 localfile? remotefile user passwd "
exit 1
}
set localfile [ lindex $argv 0 ]
set remotefile? [ lindex $argv 1 ]
set user? [ lindex $argv 2 ]
set pwd? [ lindex $argv 3 ]
set timeout 30
spawn scp ${localfile}? ${user}@${remotefile}
expect {
"*yes/no"?{ send "yes\r"; exp_continue }
"*password:"?{ send "$pwd\r" }
}
expect eof
總結
以上是生活随笔為你收集整理的上百台linux的服务器互信,批量自动建立linux服务器之间的ssh互信的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如把联想电脑计算机图标放在桌面上,thi
- 下一篇: android arm 寄存器,ARM汇