运维之道 | 使用 shell 脚本检测远程主机是否存活

1、编写需检测主机存活IP集群文本
[root@master ~]# cat ip.txt 
192.168.182.11 
192.168.182.12 
192.168.182.13 
192.168.182.14
2、编写检测主机存活脚本
[root@master ~]# vim auto_check.sh
#!/usr/bin/bash
while read ip
do
        fail_count=0
        for count in {1..2}						///定义count次数
do
        ping -c1 -W1 $ip &>/dev/null

        if [ $? -eq 0 ];then
        echo -e "\033[32mThe $ip ping is ok...\033[0m"
        echo
        break
else
        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...

在这里插入图片描述

发布了118 篇原创文章 · 获赞 13 · 访问量 9422

猜你喜欢

转载自blog.csdn.net/VillianTsang/article/details/104196319