ssh免密驗證,shell批量設置


 

ssh免密驗證,shell批量設置

 

#ssh免密驗證,shell自動設置
echo '#!/bin/sh
. /etc/init.d/functions
[[ -f /usr/bin/expect ]] || { echo "install expect";yum install expect -y; } #若沒expect則安裝
[ $? = 0 ] || { echo "expect安裝失敗";exit; } #安裝失敗則退出
PUB=/'`whoami`'/.ssh/id_dsa.pub #公鑰路徑
[[ -f $PUB ]] || { ssh-keygen -t dsa -P "" -f /'`whoami`'/.ssh/id_dsa>/dev/null 2>&1; } #若沒公鑰則生成

USER=root   #登錄用戶
PASS=123321 #登錄密碼
#expect自動交互
function EXP() {
/usr/bin/expect << EOF
set timeout 5
spawn /usr/bin/ssh-copy-id -i $PUB  $USER@$n
expect {
        "*yes/no*" { send "yes\r";exp_continue }
        "password:" { send "$PASS\n";exp_continue } 
        eof { exit }
        }
EOF
}
#
for n in $*
do
  EXP >/dev/null 2>&1
  echo $n hostname >/dev/null 2>&1
  [[ $? == 0 ]] && action "========$n" /bin/true || action "========$n" /bin/false
done
'>./ssh.sh
##################以上是生成shell文件ssh.sh##################

#hosts設置
echo '
192.168.58.21   controller1
192.168.58.22   controller2
192.168.58.23   controller3
'>>/etc/hosts

#執行shell文件,ssh批量認證
sh ./ssh.sh controller1 controller2 controller3

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM