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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

全自动化建立SSH信任

發布時間:2025/3/19 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 全自动化建立SSH信任 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#!?/bin/sh#遠程主機1(源主機) src_host=$1 src_user=$2 src_passwd=$3#遠程主機2(目標主機) dst_host=$4 dst_user=$5 dst_passwd=$6src_pub=/root/.ssh/id_rsa.pub#在遠程主機1上生成公鑰 Keygen() {expect?<<?EOFspawn?ssh?$src_user@$src_host?"test?-f?/root/.ssh/id_rsa.pub?||?echo?CheckFalse?"expect?{"password:"?{send?"$src_passwd\n"} eof?{?exit?}}expect?CheckFalse?{?exit?11?}EOFif?[?$??-ne?11?];then echo?-n?"公鑰已經存在,將直接拷貝……" else echo?-n?"公鑰不存在,開始創建……" expect?<<?EOF spawn?ssh?$src_user@$src_host?"ssh-keygen?-t?rsa?" while?{1}?{expect?{"password:"?{send?"$src_passwd\n"} "yes/no*"?{send?"yes\n"}"Enter?file?in?which?to?save?the?key*"?{send?"\n"}"Enter?passphrase*"?{send?"\n"}"Enter?same?passphrase?again:"?{send?"\n"}"Overwrite?(y/n)"?{send?"n\n"}eof?{exit} }} EOF fi }#從遠程主機1上拷貝公鑰到遠程主機2上 Get_pub?() {expect?<<?EOFspawn?scp?$src_user@$src_host:$src_pub?$dst_user@$dst_host:/root/.ssh/$src_hostexpect?{"password:"?{send?"$src_passwd\n";exp_continue}"password:"?{send?"$dst_passwd\n";exp_continue}"yes/no*"?{send?"yes\n";exp_continue}eof?{exit}}EOF }#在遠程主機2上將內容追加到authorized_keys Put_pub() {expect?<<?EOFspawn?ssh?$dst_user@$dst_host?"mkdir?-p?/root/.ssh;chmod?700?/root/.ssh;cat?/root/.ssh/$src_host?>>?/root/.ssh/authorized_keys;chmod?600?/root/.ssh/authorized_keys"expect?{"password:"?{send?"$dst_passwd\n";exp_continue}"yes/no*"?{?send?"yes\n";exp_continue}eof{exit}} EOF }Keygen Get_pub Put_pub


使用格式:

./ssh-turst.sh 192.168.1.1 root 123456 192.168.1.2 root 123456


當然也可以把這些主機IP,用戶名,密碼寫入到一個config.ini文件里

192.168.1.1 root 123456 192.168.1.2 root 123456

192.168.1.1 root 123456 192.168.1.3 root 123456


然后使用xargs命令

xargs -n6 ./ssh-trush.ssh < config.file


轉載于:https://blog.51cto.com/itech/1837519

總結

以上是生活随笔為你收集整理的全自动化建立SSH信任的全部內容,希望文章能夠幫你解決所遇到的問題。

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