本脚本是为了在RHCA学习中,实现一键重置RH236练习环境而编写:
脚本位置放在foundation0主机的root目录下,脚本名称:rh236
#!/bin/bash
# author "106140873@qq.com"
#source /content/courses/rhgs/rhgs3.1/lab-completion
#source /content/courses/rhgs/rhgs3.1/labtool.shlib
#由于只节选所需的几个函数到def_function脚本,所以上面两行注释。
source /root/def_function
#计时开始
#start=$(date +%s%N)
start=$(date +%s)
#start_ms=${start:0:16}
#一般不需要rht-vmctl fullreset all -q
rht-vmctl reset all -q
#调用函数判断所有服务器是否启动成功
#为安全起见跑两次
ifUp && ifUp
if [ $? == "0" ];
then
echo -e "033[1;36m所有服务器(${serverlist[@]})已经启动成功!033[0;39m"
fi
#如果提示课程未设置重启下classroom虚拟机
#rht-vmctl reset classroom -q
scp /root/def_function root@workstation:/root/
ssh root@workstation "source /root/def_function;ifUp"
ssh root@workstation "lab io-encryption setup"
#
for i in {a..e};do
rht-vmctl reset -q server${i}
done
#调用函数判断所有服务器是否启动成功
#为安全起见跑两次
ifUp && ifUp
if [ $? == "0" ];
then
echo -e "033[1;36m所有服务器(${serverlist[@]})已经启动成功!033[0;39m"
fi
ssh root@workstation "source /root/def_function;ifUp"
#重置servera serverb serverc serverd servere
for i in {servera,serverb,serverc,serverd,servere};do
ssh root@${i} "vgremove vg_bricks && pvremove /dev/vdb"
done
#计时结束
end=$(date +%s)
#end=$(date +%s%N)
#end_ms=${end:0:16}
#echo "scale=6;($end_ms - $start_ms)/1000000" | bc
#echo -e "033[1;36m共耗时$[fail_count*5]秒!033[0;39m"
echo -e "033[1;36m共耗时:$[($end - $start)/60]分钟$[($end - $start)%60]秒!033[0;39m"
#----------------------------------------------------------------------------------
另一个脚本是自定义函数def_function
#----------------------------------------------------------------------------------
# initialize PRINT_* counters to zero
pass_count=0 ; fail_count=0 ; success_count=0
#服务器列表
serverlist=(servera serverb serverc serverd servere workstation)
#5个函数
function print_SUCCESS() {
[[ $# -gt 0 ]] && echo -n "$@ "
echo -e "033[1;36mSUCCESS033[0;39m"
let success_count++
return 0
}
function pad {
local text="$1"
local dots='...............................................................'
printf '%s%s ' "${text}" "${dots:${#text}}"
}
function check_tcp_port {
if [[ ${#1} -gt 0 && ${#2} -gt 0 ]]; then
# Sending it to the log always returns 0
($(echo "brain" >/dev/tcp/$1/$2)) && return 0
fi
return 1
}
function wait_online {
local TARGET=$1
pad " · Waiting for ${TARGET} to become available"
while ! ping -c1 -w1 ${TARGET} &> /dev/null
do
sleep 1s
done
while ! check_tcp_port ${TARGET} 22 2>/dev/null
do
sleep 1s
done
print_SUCCESS
}
function ifUp() {
for i in ${serverlist[@]};do
wait_online $i
done
}
#-----------------------------------------------------------------------
执行脚本
sh rh236
执行时间大概是4-6分钟,根据你电脑的性能执行时间可能不同。我的实验环境是一台双E5620 CPU/160G内存/NVME固盘的ESXI上部署的。

————————————————
版权声明:本文为优快云博主「运维自动化&云计算」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://yunwei365.blog.youkuaiyun.com/article/details/99686166