背景:某日,为了验证集群模式下的节点稳定性,被指派了测试集群随机宕机稳定性的验证,手头没什么好用的工具,故自行用shell实现了一个简单的宕机小工具自娱自乐,这里记录一下:
#!/bin/bash
# 主机列表以及对应的用户名密码
HOST_LIST=('ip1' 'ip2' 'ip3')
USER_NAME=('root' 'root' 'root')
PASSWORD=( 'xxxxxxx' 'xxxxxxx' 'xxxxxx')
# 目标主机宕机重启延时(sec)
PANIC_TIME=3
# 校验用户名密码是否正确
function check_password()
{
host=$1
user_name=$2
passwd=$3
sshpass -p $3 ssh $user_name@$host 'whoami' 1>/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR: verify $host user_name and passwd fail !!!"
exit 1
fi
}
# 目标主机列表都上线后才返回
function wait_host_ready()
{
while :; do
all_ready=1
for i in ${HOST_LIST[@]}; do
ping -c 1 -W 2 $i > /dev/null
if [ $? -ne 0 ]; then