1. 程式人生 > >ssh免密 多臺機器自動安裝jdk

ssh免密 多臺機器自動安裝jdk

機器需要安裝expect 

httpd服務需要開啟

/etc/hosts 需要配置

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.203.130 test1
192.168.203.131 test2

 

 

建立boot.sh

#!/bin/bash


SERVERS="test1 test2"
PASSWORD="123456"
auto_ssh_copy_id() {
        expect -c "set timeout -1;
                 spawn ssh-copy-id $1;
                 expect {
                     *(yes/no)* {send -- yes\r;exp_continue;}
                     *assword:* {send -- $2\r;exp_continue;}
                     eof        {exit 0;}


               }";
}


ssh_copy_id_to_all(){
 for SERVER in $SERVERS
 do
    auto_ssh_copy_id $SERVER $PASSWORD
 done

ssh_copy_id_to_all

for SERVER in $SERVERS
do
   scp install.sh [email protected]$SERVER:/root
   ssh

[email protected]$SERVER /root/install.sh
done
 

建立檔案install.sh

#!/bin/bash

BASE_SERVER=192.168.203.129
yum install -y wet
wget $BASE_SERVER/apps/jdk-linux-x64.tar.gz
tar -zxvf jdk-linux-x64.tar.gz -C /usr/local
cat >> /etc/profile << EOF
export JAVA_HOME=/usr/local/jdk1.8.0_191
exprot PATH=\$PATH:$JAVA_HOME/bin
EOF