diff --git a/tools/help_scripts/check_java_home_and_path_in_batch.sh b/tools/help_scripts/check_java_home_and_path_in_batch.sh new file mode 100644 index 0000000000000000000000000000000000000000..70872fadeb2511799525cc756feb6fba2cbc1526 --- /dev/null +++ b/tools/help_scripts/check_java_home_and_path_in_batch.sh @@ -0,0 +1,115 @@ +#!/bin/bash +# 检查bishengJDK安装的三个方面 +# 1. 检查安装路径是否存在 +# 2. 检查JAVA_HOME是否和安装路径一致 +# 3. 检查java命令的全路径形式包含安装路径 + +# 安装的目标位置,即JAVA_HOME +JAVA_HOME_VALUE=/opt/software/bisheng-jdk1.8.0_412 +# 哪些服务器需要检查 每行一个服务器 格式:ip password 。其中password可以不写 默认为DEFAULT_PASSWORD +IP_FILE=ip.all +# 服务器的登陆用户 +USER=root +# 默认密码,在IP_FILE未进行配置的服务器使用的密码 +DEFAULT_PASSWORD="Huawei123" + +declare -A ip_arr +declare -A passwd_arr + +function initialize_arr() { + local index=0 + # + while read line || [[ -n ${line} ]] + do + local ip=`echo "${line}"|awk '{print $1}'` + if [[ -z $ip ]];then + continue + fi + local password=`echo "${line}"|awk '{print $2}'` + # + if [[ -z $password ]];then + password=$DEFAULT_PASSWORD + fi + ip_arr[$index]=$ip + passwd_arr[$index]=$password + index=`expr $index + 1` + done < ${IP_FILE} +} + +function connect_host_and_check_bisheng_jdk() { + local ip=$1 + local passwd=$2 +/usr/bin/expect << EOF +set timeout 10 +send_user "$USER@$ip:check java" +spawn ssh $USER@$ip +expect { + "*yes/no" {send "yes\r";exp_continue} + "Permission denied, please try again" {send_user "Permission denied to login user:$USER\n"; exit 1;} + "*password" {send "$passwd\r";exp_continue} + "*Password" {send "$passwd\r";exp_continue} + "Enter passphrase for key*" {send "$passwd\r";exp_continue} + "Last login:" {send_user "success to login\n"} + timeout {send_user "time out to login user:$USER\n"; exit 2} +} + +expect -re "\[\$#\]" { send "ls $JAVA_HOME_VALUE \r"} +expect { + -nocase "no such file or directory" {send_user "failed to copy bisheng jdk to server\n";exit 3} + -re "\[\$#\]" { + send_user "success to copy bisheng jdk to server\n"; + send "env|grep JAVA_HOME\r"; + } + timeout {send_user "time out to -d"; exit 3} +} + +expect { + "$JAVA_HOME_VALUE" {send_user "success to update JAVA_HOME\n";} + -re "\[\$#\]" {send_user "time out to env JAVA_HOME\n"; exit 4} + timeout {send_user "time out to env JAVA_HOME\n"; exit 4} +} + +expect -re "\[\$#\]" { send "which java\r"} +expect { + "${JAVA_HOME_VALUE}/bin/java" {send_user "success to update PATH\n";} + -re "\[\$#\]" {send_user "time out to which java\n"; exit 5} + timeout {send_user "time out to which java\n"; exit 5} +} +expect -re "\[\$#\]" { send "logout\r"} +expect eof +EOF +} + +function print_result() { + local index=$1 + local ret=$2 + if [[ $ret -eq 0 ]];then + echo -e "\033[32m 服务器${ip_arr[$index]} 安装成功,且修改的环境变量(JAVA_HOME)和PATH在用户${USER}生效。\033[0m" + elif [[ $ret -eq 1 ]];then + echo -e "\033[31m 服务器${ip_arr[$index]} 密码错误。\033[0m" + elif [[ $ret -eq 2 ]];then + echo -e "\033[31m 服务器${ip_arr[$index]} 登陆超时。可能原因:服务器地址不正确或者登陆返回不包含Last login或者缺少命令 \033[0m" + elif [[ $ret -eq 3 ]];then + echo -e "\033[31m 服务器${ip_arr[$index]} 中 $JAVA_HOME_VALUE 文件夹不存在。 \033[0m" + elif [[ $ret -eq 4 ]];then + echo -e "\033[31m 服务器${ip_arr[$index]} 用户${USER}的JAVA_HOME环境变量不是$JAVA_HOME_VALUE。 \033[0m" + else + echo -e "\033[31m 服务器${ip_arr[$index]} 直接使用的java命令不是${JAVA_HOME_VALUE}/bin/java。\033[0m" + fi +} + +function check_all_host() { + for ((i=0;i<${#ip_arr[@]};i++)) + do + connect_host_and_check_bisheng_jdk "${ip_arr[$i]}" ${passwd_arr[$i]} + print_result $i $? + done +} + + +function main() { + initialize_arr + check_all_host +} + +main \ No newline at end of file diff --git a/tools/help_scripts/ip.all b/tools/help_scripts/ip.all new file mode 100644 index 0000000000000000000000000000000000000000..c50cd5a253a00757d72fc651f2e4179219bffbbd --- /dev/null +++ b/tools/help_scripts/ip.all @@ -0,0 +1,5 @@ +# 本行为注释使用时删除, 每行包含两个字段,分隔符为空格,第一个字段为密码,第二个字段为密码(可不填),不填密码时使用脚本中的默认密码 +192.168.0.1 admin123 +192.168.0.2 +192.168.0.3 admin123 +192.168.0.4 \ No newline at end of file diff --git a/tools/help_scripts/unpack_and_modify.sh b/tools/help_scripts/unpack_and_modify.sh new file mode 100644 index 0000000000000000000000000000000000000000..d9619a8ac4952d3f314d3217ab554e00168246ac --- /dev/null +++ b/tools/help_scripts/unpack_and_modify.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +set -e +# bishengJDK的文件名称 +BISHENG_JDK_TAR=$1 +# bishengJDK解压后的文件夹名称 +BISHENG_DIR=$2 +# 安装的目标位置,当前最终安装在/opt/software/bisheng-jdk1.8.0_412 +TARGET_DIR=$3 + +# 在/etc/profile 中追加的内容 +ADD_JAVA_HOME="export JAVA_HOME=${TARGET_DIR}/${BISHENG_DIR}" + +tar --no-same-owner -xzf $TARGET_DIR/$BISHENG_JDK_TAR -C ${TARGET_DIR} +chmod -R 755 $TARGET_DIR + +echo $ADD_JAVA_HOME >>/etc/profile + +echo 'export PATH=${JAVA_HOME}/bin:${PATH}' >>/etc/profile + +echo "success" \ No newline at end of file diff --git a/tools/help_scripts/update_to_bisheng_jdk_in_batchs.sh b/tools/help_scripts/update_to_bisheng_jdk_in_batchs.sh new file mode 100644 index 0000000000000000000000000000000000000000..f86ee0cc77aca85df426027fa1db6b84969ae095 --- /dev/null +++ b/tools/help_scripts/update_to_bisheng_jdk_in_batchs.sh @@ -0,0 +1,161 @@ +#!/bin/bash +# 该脚本做两件事 +# 1. 复制BISHENG_TAR_DIR下的文件BISHENG_JDK_TAR到指定服务器的TARGET_DIR目录下,并解压 +# 2. 在/etc/profile中追加 export JAVA_HOME=${TARGET_DIR}/${BISHENG_DIR} 和export PATH=\${JAVA_HOME}/bin:\${PATH} + +# 存放bishengJDK的目录 +BISHENG_TAR_DIR=/home/zpp +# bishengJDK的文件名称 +BISHENG_JDK_TAR=bisheng-jdk-8u412-linux-aarch64.tar.gz +# bishengJDK解压后的文件夹名称 +BISHENG_DIR=bisheng-jdk1.8.0_412 +# 安装的目标位置,当前最终安装在/opt/software/bisheng-jdk1.8.0_412 +TARGET_DIR=/opt/software +# 哪些服务器需要安装 每行一个服务器 格式:ip password 。其中password可以不写 默认为DEFAULT_PASSWORD +IP_FILE=ip.all +# 服务器的登陆用户 +USER=root +# 默认密码,在IP_FILE未进行配置的服务器使用的密码 +DEFAULT_PASSWORD="Huawei123" +# 并行度,同时进行安装的服务器个数 +PARALLEL=10 + +# 在/etc/profile 中追加的内容 +ADD_JAVA_HOME="export JAVA_HOME=${TARGET_DIR}/${BISHENG_DIR}" +ADD_PATH='export PATH=\${JAVA_HOME}/bin:\${PATH}' + +declare -A ip_arr +declare -A passwd_arr + +function initialize_arr() { + local index=0 + # + while read line || [[ -n ${line} ]] + do + local ip=`echo "${line}"|awk '{print $1}'` + if [[ -z $ip ]];then + continue + fi + local password=`echo "${line}"|awk '{print $2}'` + # + if [[ -z $password ]];then + password=$DEFAULT_PASSWORD + fi + ip_arr[$index]=$ip + passwd_arr[$index]=$password + index=`expr $index + 1` + done < ${IP_FILE} +} + +function connect_host_and_update_to_bisheng_jdk() { + local ip=$1 + local passwd=$2 +/usr/bin/expect << EOF +set timeout 10 +send_user "$USER@$ip:create dir" +spawn ssh $USER@$ip +expect { + "*yes/no" {send "yes\r";exp_continue} + "Permission denied, please try again" {send_user "Permission denied to login user:$USER\n"; exit 1;} + "*password" {send "$passwd\r";exp_continue} + "*Password" {send "$passwd\r";exp_continue} + "Enter passphrase for key*" {send "$passwd\r";exp_continue} + "Last login:" {send_user "success to login"} + timeout {send_user "time out to login user:$USER"; exit 2} +} +expect -re "\[\$#\]" { send "mkdir -p $TARGET_DIR\r"} +expect -re "\[\$#\]" { send "logout\r"} + +set timeout 240 +expect -re "\[\$#\]" +spawn scp -r $BISHENG_TAR_DIR/$BISHENG_JDK_TAR $USER@$ip:$TARGET_DIR +expect { + "*yes/no" {send "yes\r";exp_continue} + "*password" {send "$passwd\r";exp_continue} + "*Password" {send "$passwd\r";exp_continue} + "Enter passphrase for key*" {send "$passwd\r";exp_continue} + "100%" { send_user "success to copy file\n"} + timeout {exit 2} +} + +expect -re "\[\$#\]" +spawn scp -r unpack_and_modify.sh $USER@$ip:$TARGET_DIR +expect { + "*yes/no" {send "yes\r";exp_continue} + "*password" {send "$passwd\r";exp_continue} + "*Password" {send "$passwd\r";exp_continue} + "Enter passphrase for key*" {send "$passwd\r";exp_continue} + "100%" { send_user "success to copy unpack_and_modify file\n"} + timeout {exit 2} +} + +expect -re "\[\$#\]" +spawn ssh $USER@$ip +expect { + "*yes/no" {send "yes\r";exp_continue} + "Permission denied, please try again" {send_user "Permission denied to login user:$USER"; exit 1;} + "*password" {send "$passwd\r";exp_continue} + "*Password" {send "$passwd\r";exp_continue} + "Enter passphrase for key*" {send "$passwd\r";exp_continue} + "Last login:" {send_user "success to login"} + timeout {send_user "time out to login user:$USER"; exit 2} +} +expect -re "\[\$#\]" { send "bash ${TARGET_DIR}/unpack_and_modify.sh ${BISHENG_JDK_TAR} ${BISHENG_DIR} ${TARGET_DIR}\r"} +expect { + "success" {send_user "success to unpack_and_modify"} + -re "\[\$#\]" {send_user "time out to unpack_and_modify:$USER"; exit 2} + timeout {send_user "time out to unpack_and_modify:$USER"; exit 2} +} +expect -re "\[\$#\]" { send "rm -rf $TARGET_DIR/$BISHENG_JDK_TAR\r"} +expect -re "\[\$#\]" { send "rm -rf $TARGET_DIR/unpack_and_modify.sh\r"} +expect -re "\[\$#\]" { send "logout\r"} +expect eof +EOF +} + +function print_result() { + local index=$1 + local ret=$2 + if [[ $ret -eq 0 ]];then + echo -e "\033[32m 服务器${ip_arr[$index]}安装成功。安装位置为$TARGET_DIR,并在/etc/profile文件中追加了以下配置:\n ${ADD_JAVA_HOME}\n${ADD_PATH} \033[0m" + elif [[ $ret -eq 1 ]];then + echo -e "\033[31m 服务器${ip_arr[$index]} 密码错误。\033[0m" + else + echo -e "\033[31m 服务器${ip_arr[$index]} 未安装成功。可能原因:服务器地址不正确或者登陆返回不包含Last login或者缺少命令 \033[0m" + fi +} + +function update_all_host() { + local arr_length=${#ip_arr[@]} + if [ $arr_length -gt $PARALLEL ];then + for ((i=0;i<${PARALLEL};i++)) + do + { + for ((j=i;j<${arr_length};j=j+$PARALLEL)) + do + connect_host_and_update_to_bisheng_jdk "${ip_arr[$j]}" ${passwd_arr[$j]} + print_result $j $? + done + } & + done + wait + else + for ((i=0;i<${arr_length};i++)) + do + { + connect_host_and_update_to_bisheng_jdk "${ip_arr[$i]}" ${passwd_arr[$i]} + print_result $i $? + } & + done + wait + fi + +} + + +function main() { + initialize_arr + update_all_host +} + +main \ No newline at end of file