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
