It took one day to write a script to check whether an ip address has a smooth network. it is easy to implement only the function of checking whether the network is smooth, but some restrictions are not implemented. #! /Bin/bash #################################### ######################################## ######## this script is stored on a different host, use a scheduled task to detect 192.168.10.23
It took one day to write a script to check whether an ip address has a smooth network. it is easy to implement only the function of checking whether the network is smooth, but some restrictions are not implemented.
#! /Bin/bash
######################################## ######################################## ####
# This script is stored on a different machine. it uses a scheduled task to check whether the network of 192.168.10.231 is smooth.
# To prevent frequent receipt of text messages, the following restrictions are imposed on text messages:
# Send only the first three alarm messages within one hour. that is, if the network is disconnected, only the first three messages are sent within one hour.
# This script can only check whether the network of one ip address is disconnected. you can add a loop at the beginning to check multiple ip addresses.
######################################## ######################################## ####
# Initializing environment variables
Source/root/. bash_profile
# Mail list
Mail_list = '135xxxxxxxx @ 139.com, zhangwz@xx.net'
# Ping ip addresses detected three times
Ping = 'ping-c 3 192.168.1.69 | awk 'NR = 7 {print $4 }''
# If the ping fails three times, perform the following operations:
If [$ ping-eq 0]
Then
# Check whether the mailinfo file exists. if it does not exist, create
If [! -E./mailinfo]
Then
Touch./mailinfo
Fi
# Obtain the current host Time for displaying the time of the mail body
Now_time = 'Date'
# Check whether the number of rows in./mailinfo is null. if it is null, send an email directly.
Row_num = 'WC-l./mailinfo | cut-c 1'
If [$ row_num-eq 0]
Then
Echo "email"
Echo-e "1.69 the network is disconnected. check \ n alarm time: $ now_time" | mutt-s "1.69 alarm" $ mail_list
# Update the last sending time
Date + % Y % m % d-% H % M>./mailinfo
Echo 1>./mailinfo
# If the mailinfo content is not blank, determine whether the (current time minus 1 hour) is later than the last sending time. if yes, send an email.
Else
# Obtain the date and time of the last mail.
Last_date = 'head-1./mailinfo | cut-c 1-8'
Last_time = 'head-1./mailinfo | cut-c 10-13'
# Obtain the date and time one hour ago
Current_date = 'date + % Y % m % d'
Current_time = 'date + % H % M'
# How long is the gap between the current time and the last time? add 10 # to prevent the character starting with 0 from being considered as an octal calculation
Let time_diff = 10 # $ current_time-10 # $ last_time
# Retrieve the number of sent messages
Send_times = 'tail-1./mailinfo'
# Send emails in the following four cases:
# Current date> last date, which can be sent, and then the send_times count is set to 1
If ["$ current_date"-gt "$ last_date"]
Then
Echo "email"
Echo-e "1.69 the network is disconnected. check \ n alarm time: $ now_time" | mutt-s "1.69 alarm" $ mail_list
# Update the last sending time
Date + % Y % m % d-% H % M>./mailinfo
Echo 1>./mailinfo
Fi
# If the current date is equal to the previous date and the current time is greater than the previous time, you can send the message, and set send_times to 1.
If ["$ current_date"-eq "$ last_date"-a $ time_diff-ge 100]
Then
Echo "email"
Echo-e "1.69 the network is disconnected. check \ n alarm time: $ now_time" | mutt-s "1.69 alarm" $ mail_list
# Update the last sending time
Date + % Y % m % d-% H % M>./mailinfo
Echo 1>./mailinfo
Fi
# The current date is equal to the last date and the sending count send_times is set to 1. you can send the message, and the number of sending Times is increased by 1.
If ["$ current_date"-eq "$ last_date"-a $ send_times-eq 1]
Then
Echo "email"
Echo-e "1.69 the network is disconnected. check \ n alarm time: $ now_time" | mutt-s "1.69 alarm" $ mail_list
# Update the last sending time
Date + % Y % m % d-% H % M>./mailinfo
Echo 2>./mailinfo
Fi
# The current date is equal to the last date and the sending count send_times is set to 2, which can be sent, and then the number of sending Times is increased by 1
If ["$ current_date"-eq "$ last_date"-a $ send_times-eq 2]
Then
Echo "email"
Echo-e "1.69 the network is disconnected. check \ n alarm time: $ now_time" | mutt-s "1.69 alarm" $ mail_list
# Update the last sending time
Date + % Y % m % d-% H % M>./mailinfo
Echo 3>./mailinfo
Fi
Fi
Fi