Ping to determine the monitoring host's survival

Source: Internet
Author: User

The ping command to monitor whether the host is alive, as long as a ping is normal, if the ping fails to continue to ping, three times it is considered a host outage or network problems, then you can send email alerts.

Method One:

Note: If the number of machines judged is not many, can ping three times per machine judge

#! /bin/bash

if [-e/tmp/c.txt];then

Rm-rf/tmp/c.txt

Touch/tmp/c.txt

Else

Touch/tmp/c.txt

Fi

if [-e/tmp/d.txt];then

Rm-rf/tmp/d.txt

Touch/tmp/d.txt

Else

Touch/tmp/d.txt

Fi


For i in ' SEQ 100 105 '

Do

ip=192.168.2. $i

Ping-c 3 $ip >/dev/null 2>&1

If [$?-eq 0];then

Echo $ip "is in line" >>/tmp/c.txt

Else

Echo $ip "is offline" >>/tmp/d.txt

Fi

Done

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/89/2F/wKioL1gLHlqzD_eSAABEfllztU4651.png "title=" Test26.png "alt=" Wkiol1glhlqzd_esaabefllztu4651.png "/>


Method Two:

Description: Put the number of errors in the Fail_count variable to determine that the non-ping failed three times

#! /bin/bash

if [-e/tmp/c.txt];then

Rm-rf/tmp/c.txt

Touch/tmp/c.txt

Else

Touch/tmp/c.txt

Fi

if [-e/tmp/d.txt];then

Rm-rf/tmp/d.txt

Touch/tmp/d.txt

Else

Touch/tmp/d.txt

Fi

For i in ' SEQ 100 105 '

Do

ip=192.168.2. $i

Fail_count=0

For ((i=1;i<=3;i++))

Do

If ping-c 1 $ip >/dev/null;then

echo "$ip is online" >>/tmp/c.txt

Break

Else

#echo "$ip ping is offline $i"

Let fail_count++

Fi

Done

If [$fail _count-eq 3];then

echo "$ip is offline" >>/tmp/d.txt

Fi

Done


650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/89/30/wKioL1gLI5Sh-IOVAABVZQph-bM901.png-wh_500x0-wm_3 -wmp_4-s_2043651560.png "title=" Test27.png "alt=" Wkiol1gli5sh-iovaabvzqph-bm901.png-wh_50 "/>


Method Three:

Description: Put the error IP into the array to determine if the ping failed three times

#! /bin/bash

if [-e/tmp/c.txt];then

Rm-rf/tmp/c.txt

Touch/tmp/c.txt

Else

Touch/tmp/c.txt

Fi

if [-e/tmp/d.txt];then

Rm-rf/tmp/d.txt

Touch/tmp/d.txt

Else

Touch/tmp/d.txt

Fi


For i in ' SEQ 100 105 '

Do

ip=192.168.2. $i

Num=1

While [$num-le 3]

Do

If ping-c 1 $ip >/dev/null;then

echo "$ip is online" >>/tmp/c.txt

Break

Else

# echo "$ip ping is offline $num"

fail_count[$num]= $ip

Let num++

Fi

Done

if [${#fail_count [*]}-EQ 3];then

echo "${fail_count[1]} is offline" >>/tmp/d.txt

Fi

Done

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/89/35/wKiom1gMN46jy0hVAABs8p5LiNE256.png "title=" Test28.png "alt=" Wkiom1gmn46jy0hvaabs8p5line256.png "/>


Method Four:

Description: Use for loop, because continue, if Ping is out of the loop, the ping fails if you do not jump out

#! /bin/bash

if [-e/tmp/c.txt];then

Rm-rf/tmp/c.txt

Touch/tmp/c.txt

Else

Touch/tmp/c.txt

Fi

if [-e/tmp/d.txt];then

Rm-rf/tmp/d.txt

Touch/tmp/d.txt

Else

Touch/tmp/d.txt

Fi


Ping_success_status () {

If ping-c 1 $ip >/dev/null;then

echo "$ip is online" >>/tmp/c.txt

Continue

Fi

}

For i in ' SEQ 100 105 '

Do

ip=192.168.2. $i

Ping_success_status

Ping_success_status

Ping_success_status

echo "$ip is offline" >>/tmp/d.txt

Done


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/89/33/wKioL1gMOdmj6_wVAABdfmtRxH8129.png "title=" Test29.png "alt=" Wkiol1gmodmj6_wvaabdfmtrxh8129.png "/>

Here's a little bit different from the picture above:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/89/35/wKiom1gMO96i1-HHAABew4i6Mkg111.png "title=" Test30.png "alt=" Wkiom1gmo96i1-hhaabew4i6mkg111.png "/>

Ifconfig |awk-f [: A-Z "" "]+ ' Nr==2{print $ $} ' can get an IP address



This article refers to the article:

    1. http://lizhenliang.blog.51cto.com/7876557/1736770

    2. http://blog.csdn.net/zhuying_linux/article/details/6822987

    3. http://12100846.blog.51cto.com/12090846/1863259

Ping to determine the monitoring host's survival

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.