#!/bin/bash # 三台服务器都需要免密!!! jdk_path=`pwd` #除本机外的其他两台服务器IP地址 ips=("192.168.247.136 192.168.247.134") function install_jdk(){ tar -zxvf ./pkg/jdk-8u91-linux-x64.tar.gz -C ./ echo "修改名称" mv jdk1.8.0_91 jdk1.8 # $JAVA_HOME,要用在前面加转义字符 "" cat >> /etc/profile << EOF export JAVA_HOME=$jdk_path/jdk1.8 export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export PATH=$JAVA_HOME/bin:$PATH EOF # 生效 source /etc/profile #echo "安装第二台服务器的jdk" for ip in $ips do echo "向 $ip 服务器安装jdk" # 创建文件夹 ssh root@$ip "mkdir -p $jdk_path/jdk1.8" JAVA_HOME=$jdk_path/jdk1.8 scp -r $jdk_path/jdk1.8/ root@$ip:$jdk_path/ ssh $ip "echo export JAVA_HOME=$jdk_path/jdk1.8 >> /etc/profile" ssh $ip "echo export CLASSPATH=.:'$JAVA_HOME'/jre/lib/rt.jar:'$JAVA_HOME'/lib/dt.jar:'$JAVA_HOME'/lib/tools.jar >> /etc/profile" ssh $ip "echo export PATH='$JAVA_HOME'/bin:'$PATH' >> /etc/profile" ssh $ip "source /etc/profile" sleep 5 done } function main(){ install_jdk } main