首页 > 其他 > 详细

16项目实战_判断主机存活

时间:2020-02-18 14:14:42      阅读:39      评论:0      收藏:0      [点我收藏+]

项目实战_判断主机存活

脚本目的

通过ping主机IP判断主机是否存活

脚本功能

1.ping通主机,输出"xxx is ok."

2.没有ping通主机,提供三次机会ping,ping通输出"xxx is ok.",没ping通最终输出"xxx ping is failure!"

3.通过文本传入 ip列表

脚本内容

思路1:

#/usr/bin/bash
ping_success(){
    ping -c1 -W1 ${ip} &>/dev/null
    if [ $? -eq 0 ]; then
        echo "${ip} is ok."
        continue
    fi
}

while read ip
do
    ping_success
    ping_success
    ping_success
    echo "$ip ping is failure!"
done < $1

思路2:

#!/bin/bash

while read ip
do
    for count in {1..3}; do
        ping -c1 -w1 ${ip} &> /dev/null
        if [ "$?" == "0" ]; then
            echo "ping ${ip} is ok."
            break
        else
            echo "ping ${ip} is failed: ${count}"
            if [ ${count} -eq 3 ]; then
                echo "ping ${ip} is failed."
            fi
        fi
    done
done < $1

ip.txt内容

172.22.34.78
172.22.34.12
172.22.34.33
172.22.34.45
172.22.34.23
172.22.34.124
172.22.34.139
172.22.34.8
172.22.34.32
172.22.34.170
172.22.34.160

脚本执行

[root@hadoop04 shell_awk]# bash ping_count3_3.sh  ip.txt 
172.22.34.78 is ok.
172.22.34.12 ping is failure!
172.22.34.33 ping is failure!
172.22.34.45 ping is failure!
172.22.34.23 is ok.
172.22.34.124 is ok.
172.22.34.139 ping is failure!
172.22.34.8 ping is failure!
172.22.34.32 ping is failure!
172.22.34.170 ping is failure!
172.22.34.160 ping is failure!

16项目实战_判断主机存活

原文:https://www.cnblogs.com/ElegantSmile/p/12325803.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!