You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/bin/bash
while [ $# -gt 0 ]; do
case "$1" in
--username=*)
username="${1#*=}"
;;
*)
echo "Invalid argument: $1"
exit 1
;;
esac
shift
done
# 目标机器列表,JAVA我都跑过了ip故意改错了免得有人错误运行
targets=(
"X10.10.241.203"
"X10.10.241.204"
"X10.10.241.205"
"X10.10.241.206"
"X10.10.241.207"
"X10.10.241.209"
"X10.10.241.211"
"X10.10.241.212"
"X10.10.241.213"
"X10.10.241.214"
"X10.10.241.215"
"X10.10.241.217"
"X10.10.241.218"
"X10.10.241.220"
"X10.10.241.221"
"X10.10.241.222"
)
#这里展示的离线安装如果在线安装只需要step 2的执行命令
# 本地文件路径
local_file="jdk.tar.gz"
# 解压后的实际目录名称
fact_name="jdk1.8.0_271"
# 所有机器建好了临时目录/home/sptcc/temp/ 和 安装目录/opt/module
# 远程临时目录
remote_temp_path="/home/sptcc/temp/"
# 远程安装目录
remote_module_path="/opt/module/"
remote_install_path="java"
# 远程安装包的位置
remote_file="${remote_temp_path}${local_file}"
# 安装后的环境变量(如果配置错误了,记得把/etc/environment中错误内容删除避免影响
# 如果软件相同但不同机器环境变量有区别可IF条件动态处理
remote_env_path="${remote_module_path}${remote_install_path}/${fact_name}"
# Set default values for missing parameters
if [ -z "$username" ]; then
username="root"
fi
# step 1.拷贝命令
scp_command="$local_file $username@$target:$remote_temp_path"
# step 2.执行命令
command=$(cat <<EOF
cd $remote_module_path && \
mkdir -p $remote_install_path && \
tar -xzf $remote_file -C ./$remote_install_path && \
rm -rf $remote_file && \
sudo echo "JAVA_HOME=$remote_env_path" >> /etc/environment && \
sudo echo "PATH=\"$remote_env_path/bin:$PATH\"" >> /etc/environment && \
source /etc/environment && \
java -version
EOF
)
# 循环遍历目标机器列表
for target in "${targets[@]}"
do
echo "Copying $jdk_file to $target..."
# 拷贝JDK文件到目标机器并安装
echo "step 1.scp_command >> $scp_command"
echo "step 2.command >> $command"
scp $scp_command && \
ssh $username@$target "$command"
echo "installed on $target"
done
echo "installation completed on all machines"