zoukankan      html  css  js  c++  java
  • 服务器存活脚本分析

    在linux系统中,可以使用ping命令检测主机状态,根据返回的状态信息,判断当前主机是活动,还是已经宕机了。

    下面是判断网络中主机存活状态的shell第一个脚本,具体如下所示:

    方法一:使用for循环

    [root@tianyun ~]# vim ping_count3_1.sh

    #!/usr/bin/bash

    ip_list=”10.18.40.1 10.18.42.127 10.18.42.8 10.18.42.5”

    #循环ip_list中的ip各一次

    for ip in $ip_list

    do

        #对每个ip各ping三次判断存活状态

        for count in {1..3}

        do

            ping –c1 –W1 $ip &>/dev/null

            #如果ping通就退出,ping不通统计失败的次数

            if [ $? –eq 0 ];then

                echo “$ip ping is ok.”

                break

            else

                echo “$ip ping is failure: $count”

                #利用数组的方式统计失败的次数

                fail_count[$count]=$ip

            fi

        done

        #判断fail_count数组的元素个数,如果个数为3,说明这个ip是不通的

        if [ ${#fail_count[*]} –eq 3 ];then

            echo “${fail_count[1]} ping is failure!”

            unset fail_count[*]

        fi

    done

    第一种方法执行结果如下:

     [root@tianyun ~]# chmod a+x ping_count3_1.sh

     [root@tianyun ~]# ./ping_count3_1.sh

    10.18.40.1 ping is ok.

    10.18.42.127 ping is ok.

    10.18.42.8 ping is failure: 1

    10.18.42.8 ping is failure: 2

    10.18.42.8 ping is failure: 3

    10.18.42.8 ping is failure!

    10.18.42.5 ping is failure: 1

    10.18.42.5 ping is failure: 2

    10.18.42.5 ping is failure: 3

    10.18.42.5 ping is failure!

    方法二:使用while循环

    [root@tianyun ~]# vim ip.txt

    10.18.40.1

    10.18.42.127

    10.18.42.4

    10.18.42.8

    While循环脚本如下:

    [root@tianyun ~]# vim ping_count3_1.sh

    #!/usr/bin/bash

    #ip_list=”10.18.40.1 10.18.42.127 10.18.42.8 10.18.42.5”

    #循环ip_list中的ip各一次

    While read ip

    do

        #对每个ip各ping三次判断存活状态

        for count in {1..3}

        do

            ping –c1 –W1 $ip &>/dev/null

            #如果ping通就退出,ping不通统计失败的次数

            if [ $? –eq 0 ];then

                echo “$ip ping is ok.”

                break

            else

                echo “$ip ping is failure: $count”

                #利用数组的方式统计失败的次数

                fail_count[$count]=$ip

            fi

        done

        #判断fail_count数组的元素个数,如果个数为3,说明这个ip是不通的

        if [ ${#fail_count[*]} –eq 3 ];then

            echo “${fail_count[1]} ping is failure!”

            unset fail_count[*]

        fi

    done <ip.txt

    第二种方法执行结果如下:

     [root@tianyun ~]# ./ping_count3_1.sh

    10.18.40.1 ping is ok

    10.18.42.127 ping is ok

    10.18.42.4 ping is failure: 1

    10.18.42.4 ping is failure: 2

    10.18.42.4 ping is failure: 3

    10.18.42.4 ping is failure!

    10.18.42.8 ping is failure: 1

    10.18.42.8 ping is failure: 2

    10.18.42.8 ping is failure: 3

    10.18.42.8 ping is failure!

    下面是判断网络中主机存活状态的shell第二个脚本,具体如下所示:

    [root@tianyun ~]# vim ip.txt

    10.18.40.1

    10.18.42.127

    10.18.42.4

    10.18.42.8

    [root@tianyun ~]# vim ping_count3_2.sh

    #!/usr/bin/bash

    while read ip

    do

        #定义失败次数的初值,计算失败的次数

        for i 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: $i”

                let fail_count++

            fi

        done

        if [ $fail_count –eq 3 ]; then

            echo “$ip ping is failure!”

        fi

    done <ip.txt

    执行结果如下所示:

    [root@tianyun ~]# chmod a+x ping_count3_2.sh

     [root@tianyun ~]# ./ping_count3_2.sh

    10.48.40.1 ping ok.

    10.18.42.127 ping is ok.

    10.48.42.4 ping is failure: 1

    10.48.42.4 ping is failure: 2

    10.18.42.4 ping is failure: 3

    10.18.42.4 ping is failure!

    10.18.42.8 ping is failure: 1

    10.18.42.8 ping is failure: 2

    10.18.42.8 ping is failure: 3

    10.18.42.8 ping is failure!

    下面是判断网络中主机存活状态的shell第三个脚本,具体如下所示:

    [root@tianyun ~]# vim ping_count3_3.sh

    #!/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 <ip.txt

    执行结果如下:

    [root@tianyun ~]# chmod a+x ping_count3_3.sh

     [root@tianyun ~]# ./ping_count3_3.sh

    10.18.40.1 is ok.

    10.18.42.127 is ok.

    10.18.42.4 ping is failure!

    10.18.42.8 ping is failure!

  • 相关阅读:
    CF-1111 (2019/2/7 补)
    CF-1096C Polygon for the Angle
    CF-1100 E Andrew and Taxi
    CF-1099 D. Sum in the tree
    sscanf的使用
    CF-1082(渣渣只做了前三个)
    UVA-10817- Headmaster's Headache(状压DP)
    UVA-1220-Party at Hali-Bula && UVA-1218-Perfect Service(树形DP)
    CF-1072-C. Cram Time(贪心,数学)
    CF-1027-B. Curiosity Has No Limits
  • 原文地址:https://www.cnblogs.com/momenglin/p/10294914.html
Copyright © 2011-2022 走看看