#!/bin/bash #定义颜色变量 RED='\E[1;31m' # 红 GREEN='\E[1;32m' # 绿 YELOW='\E[1;33m' # 黄 BLUE='\E[1;34m' # 蓝 PINK='\E[1;35m' # 粉红 SHAN='\E[33;5m' # 黄色闪烁警示 RES='\E[0m' # 清除颜色 #定义颜色动作 SETCOLOR_SUCCESS="echo -en \\E[1;32m" SETCOLOR_FAILURE="echo -en \\E[1;31m" SETCOLOR_WARNING="echo -en \\E[1;33m" SETCOLOR_NORMAL="echo -en \\E[0;39m" #定义变量 date=` date +'%Y%m%d_%H%M%S' ` date_init=` date +'%Y%m%d %H:%M:%S' ` scripts_path='/root/scripts' original='original.conf' trans_file='trans_server.txt' aliyun_file='visit_aliyun_server.txt' passwd_file='password.txt' nc_file='nc_ipport.txt' aliyun_nc_file='aliyun_nc_ipport.txt' nc_yes_no='nc_yes_no.log' nc_rpm='nmap-ncat-6.40-19.el7.x86_64.rpm' ssh_port=22 #准备开始 echo -e "\n\n***********************************************" echo -e "* ${YELOW} 服务器清除缓存开始! ${RES} *" echo -e "* ${YELOW} ${date_init} ${RES} *" echo -e "***********************************************\n\n" #判断系统本身是否已安装nc,sshpass which nc > /dev/null 2>&1 if [ $? -ne 0 ]; then echo -e "${RED} 请先安装nc ${RES}" exit fi which sshpass > /dev/null 2>&1 if [ $? -ne 0 ]; then echo -e "${RED} 请先安装sshpass ${RES}" exit fi #判断目录是否存在 if [ ! -d ${scripts_path}/conf ] || [ ! -d ${scripts_path}/files ] || [ ! -d ${scripts_path}/build ] || [ ! -d ${scripts_path}/rpm ] || [ ! -d ${scripts_path}/backup ] ; then mkdir -p ${scripts_path}/conf ${scripts_path}/files ${scripts_path}/build ${scripts_path}/rpm ${scripts_path}/backup fi #判断配置文件是否存在 if [ ! -f ${scripts_path}/conf/${original} ] ; then echo -e "${RED} 配置文件${scripts_path}/conf/${original}不存在,请先添加配置文件。 ${RES}" exit fi if [ ! -f ${scripts_path}/rpm/${nc_rpm} ] ; then ehco -e "${RED} nc包${scripts_path}/rpm/${nc_rpm}不存在,请先上传安装包。 ${RES} " exit fi #判断本机IP在original.conf配置文件里面是否存在,如果存在就删除该行 ip a | grep "inet " | awk '{print $2}' | awk -F'/' '{print $1}' > ${scripts_path}/build/local_ip.txt for local_ip in $(cat ${scripts_path}/build/local_ip.txt) ; do sed -i "/${local_ip}/d" ${scripts_path}/conf/${original} done rm -rf ${scripts_path}/build/local_ip.txt #扫描配置文件内容到对应的文件 grep ${trans_file%.txt} ${scripts_path}/conf/${original} | grep -v ${trans_file} | grep -v ^# | awk -F, '{print $1}' > ${scripts_path}/build/${trans_file} grep ${aliyun_file%.txt} ${scripts_path}/conf/${original} | grep -v ${aliyun_file} | grep -v ^# | awk -F, '{print $1}' > ${scripts_path}/build/${aliyun_file} grep ${passwd_file%.txt} ${scripts_path}/conf/${original} | grep -v ${passwd_file} | grep -v ^# | awk -F, '{print $1}' > ${scripts_path}/build/${passwd_file} grep ${nc_file%.txt} ${scripts_path}/conf/${original} | grep -v ${nc_file} | grep -v ${aliyun_nc_file%.txt} | grep -v ^# | awk -F, '{print $1}' > ${scripts_path}/build/${nc_file} grep ${aliyun_nc_file%.txt} ${scripts_path}/conf/${original} | grep -v ${aliyun_nc_file} | grep -v ^# | awk -F, '{print $1}' > ${scripts_path}/build/${aliyun_nc_file} #判断文件是否为空 echo > ${scripts_path}/files/${nc_yes_no} if [ ! -s ${scripts_path}/build/${trans_file} ] ; then echo -e "${RED} 交易服务器IP为空 ${RES}" echo "交易服务器IP为空" >> ${scripts_path}/files/${nc_yes_no} exit fi if [ ! -s ${scripts_path}/build/${passwd_file} ] ; then echo -e "${RED} 服务器登录密码为空 ${RES}" echo "服务器登录密码为空" >> ${scripts_path}/files/${nc_yes_no} exit fi if [ ! -s ${scripts_path}/build/${nc_file} ] ; then echo -e "${RED} 外访IP端口号为空 ${RES}" echo "访IP端口号为空" >> ${scripts_path}/files/${nc_yes_no} exit fi #显示要测试的服务器IP echo -e "\n本次要测试的服务器IP为:" >> ${scripts_path}/files/${nc_yes_no} awk '{print $0}' ${scripts_path}/build/${trans_file} >> ${scripts_path}/files/${nc_yes_no} echo -e "\n===============================================\n" >> ${scripts_path}/files/${nc_yes_no} #测试交易服务器是否能ssh远程连接 array_ssherror=() for trans_ip in $(cat ${scripts_path}/build/${trans_file}) ; do nc -zw2 ${trans_ip} ${ssh_port} if [ $? -eq 0 ]; then echo -e "${GREEN} ${trans_ip}:${ssh_port} 端口访问成功 ${RES}" else echo -e "${RED} ${trans_ip}:${ssh_port} 端口访问失败 ${RES}" echo "${trans_ip}:${ssh_port} 端口访问失败" >> ${scripts_path}/files/${nc_yes_no} sed -i "/${trans_ip}/d" ${scripts_path}/build/${trans_file} array_ssherror+=("${trans_ip}:${ssh_port}--端口访问失败") fi done #分割线 echo -e "\n===============================================\n" #测试交易服务器是否能正常登录 array_login=() for trans_ip in $(cat ${scripts_path}/build/${trans_file}) ; do sshpass -f ${scripts_path}/build/${passwd_file} ssh -o StrictHostKeyChecking=no -o ConnectTimeout=2 root@${trans_ip} 'ls' > /dev/null 2>&1 if [ $? -eq 0 ]; then echo -e "${GREEN} ${trans_ip} 登录成功 ${RES}" else echo -e "${RED} ${trans_ip} 用户名或密码错误 ${RES}" echo "${trans_ip} 用户名或密码错误" >> ${scripts_path}/files/${nc_yes_no} sed -i "/${trans_ip}/d" ${scripts_path}/build/${trans_file} array_login+=("${trans_ip}--用户名或密码错误") fi done #分割线 echo -e "\n===============================================\n" #检查远程服务器是否已经安装nc,如果没有安装,则拷贝过去进行安装,如果还是失败,就跳过 array_nc=() for trans_ip in $(cat ${scripts_path}/build/${trans_file}) ; do sshpass -f ${scripts_path}/build/${passwd_file} ssh -o StrictHostKeyChecking=no root@${trans_ip} 'which nc' > /dev/null 2>&1 if [ $? -ne 0 ]; then sshpass -f ${scripts_path}/build/${passwd_file} scp -o StrictHostKeyChecking=no ${scripts_path}/rpm/${nc_rpm} root@${trans_ip}:/root sshpass -f ${scripts_path}/build/${passwd_file} ssh -o StrictHostKeyChecking=no root@${trans_ip} 'rpm -ivh nmap-ncat-6.40-19.el7.x86_64.rpm' > /dev/null 2>&1 if [ $? -ne 0 ]; then echo -e "${RED} ${trans_ip} nc安装失败! ${RES}" echo "${trans_ip} nc安装失败!" >> ${scripts_path}/files/${nc_yes_no} sed -i "/${trans_ip}/d" ${scripts_path}/build/${trans_file} array_nc+=("${trans_ip}--nc安装失败!") fi fi done #备份重启前的路由表并且重启 for trans_ip in $(cat ${scripts_path}/build/${trans_file}) ; do sshpass -f ${scripts_path}/build/${passwd_file} ssh -o StrictHostKeyChecking=no root@${trans_ip} 'route -n > /root/route.txt' sshpass -f ${scripts_path}/build/${passwd_file} scp -o StrictHostKeyChecking=no root@${trans_ip}:/root/route.txt ${scripts_path}/files/${trans_ip}_old.txt sshpass -f ${scripts_path}/build/${passwd_file} ssh -o StrictHostKeyChecking=no root@${trans_ip} 'reboot' > /dev/null 2>&1 echo -e "${YELOW} ${trans_ip} 正在重启... ${RES}" done #分割线 echo -e "\n===============================================\n" #检测服务器是否重启成功 array_reboot=() reboot_num=0 for trans_ip in $(cat ${scripts_path}/build/${trans_file}) ; do #设置倒计时时间(秒) end_time=$(( $(date +%s) + 600 )) # 600秒后结束倒计时的时间戳(当前时间戳+600秒) #实时倒计时循环 while [ "$(date +%s)" -lt "$end_time" ] ; do current_time=$(( $(date +%s) - $(date +%s -d "1970-01-01 UTC") )) #当前时间戳减去1970年1月1日UTC的时间戳,得到当前时间(秒) remaining=$(( end_time - current_time )) #计算剩余时间(秒) sshpass -f ${scripts_path}/build/${passwd_file} ssh -o StrictHostKeyChecking=no -o ConnectTimeout=2 -n root@${trans_ip} 'ls' > /dev/null 2>&1 if [ $? -ne 0 ]; then display_array=( "正在重启. " "正在重启.. " "正在重启..." ) for dis_cat in "${display_array[@]}"; do current_time=$(( $(date +%s) - $(date +%s -d "1970-01-01 UTC") )) #当前时间戳减去1970年1月1日UTC的时间戳,得到当前时间(秒) remaining=$(( end_time - current_time )) #计算剩余时间(秒) printf " %s 倒计时:%03d秒\r" "服务器${trans_ip} ${dis_cat}" "${remaining}" #\r使光标回到行首,实现实时更新显示,不换行 sleep 1 echo -ne "\033[2K" done else echo -e "${GREEN} ${trans_ip} 重启成功 ${RES} " break fi done sleep 1 if [ "$(date +%s)" -ge "$end_time" ] ; then echo -ne "\033[2K" echo -e "${RED} ${trans_ip} 重启失败 ${RES} " reboot_num=$(( reboot_num + 1 )) array_reboot+=("${trans_ip}--重启失败") fi done if [ ${reboot_num} -ne 0 ] ; then echo -e "\n${RED} 有${reboot_num}个服务器重启失败 ${RES}" echo "有${reboot_num}个服务器重启失败" >> ${scripts_path}/files/${nc_yes_no} fi #echo -e "\n${GREEN} 服务器重启中,请等待10分钟... ${RES}\n" ##设置倒计时时间(秒) #end_time=$(( $(date +%s) + 600 )) # 600秒后结束倒计时的时间戳(当前时间戳+600秒) ##实时倒计时循环 #while [ "$(date +%s)" -lt "$end_time" ]; do # current_time=$(( $(date +%s) - $(date +%s -d "1970-01-01 UTC") )) #当前时间戳减去1970年1月1日UTC的时间戳,得到当前时间(秒) # remaining=$(( end_time - current_time )) #计算剩余时间(秒) # printf " 倒计时:%03d秒\r" $remaining #\r使光标回到行首,实现实时更新显示,不换行 # sleep 1 #等待1秒 #done #echo " 倒计时结束!" #分割线 #echo -e "\n===============================================\n" ##检测服务器是否重启成功 #reboot_num=0 #for trans_ip in $(cat ${scripts_path}/build/${trans_file}) ; do # sshpass -f ${scripts_path}/build/${passwd_file} ssh -o StrictHostKeyChecking=no -o ConnectTimeout=2 root@${trans_ip} 'ls' > /dev/null 2>&1 # if [ $? -eq 0 ]; then # echo -e "${GREEN} ${trans_ip} 重启成功 ${RES}" # else # echo -e "${RED} ${trans_ip} 重启失败 ${RES}" # echo "${trans_ip} 重启失败" >> ${scripts_path}/files/${nc_yes_no} # sed -i "/${trans_ip}/d" ${scripts_path}/build/${trans_file} # reboot_num=$(( reboot_num + 1 )) # fi #done #if [ ${reboot_num} -ne 0 ] ; then # echo -e "\n${RED} 有${reboot_num}个服务器重启失败 ${RES}" # echo "有${reboot_num}个服务器重启失败" >> ${scripts_path}/files/${nc_yes_no} #fi #分割线 echo -e "\n===============================================\n" #备份重启后的路由表并且对比看是否一致 array_diffroute=() route_num=0 for trans_ip in $(cat ${scripts_path}/build/${trans_file}) ; do sshpass -f ${scripts_path}/build/${passwd_file} ssh -o StrictHostKeyChecking=no root@${trans_ip} 'route -n > /root/route.txt' sshpass -f ${scripts_path}/build/${passwd_file} scp -o StrictHostKeyChecking=no root@${trans_ip}:/root/route.txt ${scripts_path}/files/${trans_ip}_new.txt awk '{if ($5 != "0") print $0}' ${scripts_path}/files/${trans_ip}_old.txt | awk '{$5 = ""; $6 = ""; $7 = ""; print $0}' > ${scripts_path}/files/${trans_ip}_old_modify.txt awk '{if ($5 != "0") print $0}' ${scripts_path}/files/${trans_ip}_new.txt | awk '{$5 = ""; $6 = ""; $7 = ""; print $0}' > ${scripts_path}/files/${trans_ip}_new_modify.txt diff ${scripts_path}/files/${trans_ip}_old_modify.txt ${scripts_path}/files/${trans_ip}_new_modify.txt if [ $? -eq 0 ]; then echo -e "${GREEN} ${trans_ip} 路由没变 ${RES}" else echo -e "${RED} ${trans_ip} 路由前后不一致 ${RES}" echo "${trans_ip} 路由前后不一致" >> ${scripts_path}/files/${nc_yes_no} echo -e "\n===============================================\n" >> ${scripts_path}/files/${nc_yes_no} echo -e "${trans_ip} 重启前路由\n" >> ${scripts_path}/files/${nc_yes_no} awk '{print $0}' ${scripts_path}/files/${trans_ip}_old.txt >> ${scripts_path}/files/${nc_yes_no} echo -e "\n===============================================\n" >> ${scripts_path}/files/${nc_yes_no} echo -e "${trans_ip} 重启后路由\n" >> ${scripts_path}/files/${nc_yes_no} awk '{print $0}' ${scripts_path}/files/${trans_ip}_new.txt >> ${scripts_path}/files/${nc_yes_no} echo -e "\n===============================================\n" >> ${scripts_path}/files/${nc_yes_no} echo -e "\n===============================================\n" route_num=$(( route_num + 1 )) array_diffroute+=("${trans_ip}--路由前后不一致") fi rm -rf ${scripts_path}/files/${trans_ip}_old_modify.txt rm -rf ${scripts_path}/files/${trans_ip}_new_modify.txt done if [ ${route_num} -ne 0 ] ; then echo -e "\n${RED} 有${route_num}个服务器路由不一致 ${RES}" echo "有${route_num}个服务器路由不一致" >> ${scripts_path}/files/${nc_yes_no} fi #分割线 echo -e "\n===============================================\n" #测试端口连通性 array_port=() for trans_ip in $(cat ${scripts_path}/build/${trans_file}) ; do echo -e "${GREEN} 测试${trans_ip}服务器端口联通性 ${RES}\n" echo -e "测试${trans_ip}服务器端口联通性\n" >> ${scripts_path}/files/${nc_yes_no} while IFS=' ' read -r ip port desc || [[ -n "$ip" && -n "$port" && -n "$desc" ]] ; do sshpass -f ${scripts_path}/build/${passwd_file} ssh -o StrictHostKeyChecking=no -n root@${trans_ip} "nc -zw2 ${ip} ${port}" if [ $? -eq 0 ]; then echo -e "${GREEN} ${desc} ${ip} ${port} 端口访问成功 ${RES}" echo -e " ${desc} ${ip} ${port} 端口访问成功" >> ${scripts_path}/files/${nc_yes_no} else echo -e "${RED} ${desc} ${ip} ${port} 端口访问失败。。。。。。 ${RES}" echo -e " ${desc} ${ip} ${port} 端口访问失败。。。。。。" >> ${scripts_path}/files/${nc_yes_no} array_port+=("${trans_ip}访问${desc}${ip}:${port}失败") fi done < ${scripts_path}/build/${nc_file} if [ -s ${scripts_path}/build/${aliyun_file} ] ; then for aliyun_ip in $(cat ${scripts_path}/build/${aliyun_file}) ; do if [ "${trans_ip}" = "${aliyun_ip}" ] ; then while IFS=' ' read -r ip port desc || [[ -n "$ip" && -n "$port" && -n "$desc" ]] ; do sshpass -f ${scripts_path}/build/${passwd_file} ssh -o StrictHostKeyChecking=no -n root@${trans_ip} "nc -zw2 ${ip} ${port}" if [ $? -eq 0 ]; then echo -e "${GREEN} ${desc} ${ip} ${port} 端口访问成功 ${RES}" echo -e " ${desc} ${ip} ${port} 端口访问成功" >> ${scripts_path}/files/${nc_yes_no} else echo -e "${RED} ${desc} ${ip} ${port} 端口访问失败。。。。。。 ${RES}" echo -e " ${desc} ${ip} ${port} 端口访问失败。。。。。。" >> ${scripts_path}/files/${nc_yes_no} array_port+=("${trans_ip}访问${desc}${ip}:${port}失败") fi done < ${scripts_path}/build/${aliyun_nc_file} fi done fi echo -e "\n===============================================\n" echo -e "\n===============================================\n" >> ${scripts_path}/files/${nc_yes_no} done #展示报错服务器 if [ ${#array_ssherror[@]} -ne 0 ]; then for element_ssherror in "${array_ssherror[@]}" ; do echo -e "${RED} ${element_ssherror} ${RES}" done echo -e "\n===============================================\n" fi if [ ${#array_login[@]} -ne 0 ]; then for element_login in "${array_login[@]}" ; do echo -e "${RED} ${element_login} ${RES}" done echo -e "\n===============================================\n" fi if [ ${#array_nc[@]} -ne 0 ]; then for element_nc in "${array_nc[@]}" ; do echo -e "${RED} ${element_nc} ${RES}" done echo -e "\n===============================================\n" fi if [ ${#array_reboot[@]} -ne 0 ]; then for element_reboot in "${array_reboot[@]}" ; do echo -e "${RED} ${element_reboot} ${RES}" done echo -e "\n===============================================\n" fi if [ ${#array_diffroute[@]} -ne 0 ]; then for element_diffroute in "${array_diffroute[@]}" ; do echo -e "${RED} ${element_diffroute} ${RES}" done echo -e "\n===============================================\n" fi if [ ${#array_port[@]} -ne 0 ]; then for element_port in "${array_port[@]}" ; do echo -e "${RED} ${element_port} ${RES}" done echo -e "\n===============================================\n" fi #备份必要的文件 echo -e "\n${GREEN} 文件已备份到目录${scripts_path}/backup/${date}_nc下 ${RES}" echo "文件已备份到目录${scripts_path}/backup/${date}_nc下" >> ${scripts_path}/files/${nc_yes_no} echo -e "${GREEN} ${scripts_path}/backup/${nc_yes_no}是报错和连通性测试结果文件 ${RES}\n" echo "${scripts_path}/backup/${nc_yes_no}是报错和连通性测试结果文件" >> ${scripts_path}/files/${nc_yes_no} mkdir -p ${scripts_path}/backup/${date}_nc cp -rf ${scripts_path}/files/${nc_yes_no} ${scripts_path}/backup mv ${scripts_path}/files ${scripts_path}/build ${scripts_path}/backup/${date}_nc cp -rf ${scripts_path}/conf/${original} ${scripts_path}/backup/${date}_nc/build