日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

上百台linux的服务器互信,批量自动建立linux服务器之间的ssh互信

發布時間:2024/9/27 linux 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 上百台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互信的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。