[root@master ~]# vim auto_check.sh
#!/usr/bin/bash
while read ip
do
fail_count=0for count in{1..2}///定义count次数do
ping -c1 -W1 $ip &>/dev/nullif[ $?-eq 0];then
echo -e "\033[32mThe $ip ping is ok...\033[0m"
echo
breakelse
echo -e "\033[31mThe server ping is failurl: $ip\033[0m"let fail_count++
fi
done
if[ $fail_count -eq 2];then ///定义失败后执行次数
echo -e "\033[31mThe $ip ping is failure...\033[0m"
echo
fi
done < ip.txt
3、给脚本赋予可执行权限
[root@master ~]# chmod +x auto_check.sh
4、执行脚本自动检测主机是否存活
[root@master ~]# sh auto_check.sh
The 192.168.182.11 ping is ok...
The 192.168.182.12 ping is ok...
The server ping is failurl:192.168.182.13
The server ping is failurl:192.168.182.13
The 192.168.182.13 ping is failure...
The server ping is failurl:192.168.182.14
The server ping is failurl:192.168.182.14
The 192.168.182.14 ping is failure...