Shell script implementation detects an IP network unblocked (including SMS alarm function) _linux shell

Source: Internet
Author: User
Tags current time

It took 1 days to write a script to detect whether an IP network is unblocked, but to detect whether the network is unblocked is easy to implement, but it is not good to add some restrictions.

Copy Code code as follows:

#!/bin/bash
####################################################################################
#本脚本放于异机, the use of timed tasks to detect whether the network 192.168.10.231, if not general text message alarm
#为防止频繁收到短信, the following restrictions are made for sending text messages:
#一小时内只发前三条告警短信, that is, if the network is not working, then send only the first three messages per hour.
#本脚本只能检测一个ip是否网络断开, you can add a loop at the beginning to detect multiple IP
####################################################################################
#初始化环境变量
Source/root/.bash_profile
#邮件发送列表
Mail_list= ' 135xxxxxxxx@139.com,zhangwz@xx.net '
#ping三次检测的ip
Ping= ' ping-c 3 192.168.1.69|awk ' nr==7 {print $} '
#如果三次均没ping通, the following actions are done
If [$ping-eq 0]
Then
#检查mailinfo文件是否存在, does not exist is established
if [!-E./mailinfo]
Then
Touch./mailinfo
Fi
#获取当前主机时间, display of time for message body
Now_time= ' Date '
#检查./mailinfo the number of rows is empty, and if empty, send the message directly
row_num= ' Wc-l/mailinfo | Cut-c 1 '
If [$row _num-eq 0]
Then
echo "Send an email"
ECHO-E "1.69 Network is disconnected, please check \ nthe alarm time: $now _time" | Mutt-s "1.69 Alarm" $mail _list
#更新最后一次发送时间
Date +%y%m%d-%h%m >/mailinfo
Echo 1 >>/mailinfo
#如果mailinfo内容不为空, determine whether (the current time minus 1 hours) is greater than the last time sent, and if so, send the message
Else
#获取上次发邮件的日期和时间
Last_date= ' Head-1/mailinfo | Cut-c 1-8 '
Last_time= ' Head-1/mailinfo | Cut-c 10-13 '
#获取1小时前的日期和时间
Current_date= ' Date +%y%m%d '
Current_time= ' Date +%h%m '
#计算当前时间和上次时间差距多久, 10# is designed to prevent 0 characters from starting as 8.
Let time_diff=10# $current _time-10# $last _time
#获取发送次数
Send_times= ' tail-1./mailinfo '
#以下四种情况发邮件:
#当前日期 > Last date, can be sent, and then send Count Send_times set to 1
If ["$current _date"-gt "$last _date"]
Then
echo "Send an email"
ECHO-E "1.69 Network is disconnected, please check \ nthe alarm time: $now _time" | Mutt-s "1.69 Alarm" $mail _list
#更新最后一次发送时间
Date +%y%m%d-%h%m >/mailinfo
Echo 1 >>/mailinfo
Fi
#当前日期等于上次日期 and this time is greater than the last time 1 hours, can be sent, and then send Count Send_times set to 1
If ["$current _date"-eq "$last _date"-a $time _diff-ge 100]
Then
echo "Send an email"
ECHO-E "1.69 Network is disconnected, please check \ nthe alarm time: $now _time" | Mutt-s "1.69 Alarm" $mail _list
#更新最后一次发送时间
Date +%y%m%d-%h%m >/mailinfo
Echo 1 >>/mailinfo
Fi
#当前日期等于上次日期 and send Count send_times to 1, can be sent, and then send the number of times plus 1
If ["$current _date"-eq "$last _date"-a $send _times-eq 1]
Then
echo "Send an email"
ECHO-E "1.69 Network is disconnected, please check \ nthe alarm time: $now _time" | Mutt-s "1.69 Alarm" $mail _list
#更新最后一次发送时间
Date +%y%m%d-%h%m >/mailinfo
Echo 2 >>/mailinfo
Fi
#当前日期等于上次日期 and send Count Send_times to 2, can be sent, and then send the number of times plus 1
If ["$current _date"-eq "$last _date"-A $send _times-eq 2]
Then
echo "Send an email"
ECHO-E "1.69 Network is disconnected, please check \ nthe alarm time: $now _time" | Mutt-s "1.69 Alarm" $mail _list
#更新最后一次发送时间
Date +%y%m%d-%h%m >/mailinfo
Echo 3 >>/mailinfo
Fi
Fi
Fi

Related Article

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.