Linux系统Shell编程及自动化运维实现-综合实战
实战1
判断主机存活三次机会
- 使用三种方式书写ping脚本
1.主机地址已知。
2.在线主机提示一次在线即可。
3.不在线主机,需要ping失败超过三次,再确认显示主机不在线。 - 示例1
#!/usr/bin/bash
#ip_list="10.18.40.1 10.18.42.127 10.18.42.8 10.18.42.5"
while read ip
do
for count in {
1..3}
do
ping -c1 -W1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "$ip ping is ok."
break
else
echo "$ip ping is failure: $count"
fail_count[$count]=$ip
fi
done
if [ ${
#fail_count[*]} -eq 3 ] ;then
echo "${fail_count[1]} ping is failure!"
unset fail_count[*]
fi
done <ip.txt
- 示例2
#!/usr/bin/bash
while read ip
do
fail_count=0
for i in {
1..3