ping nowhere on the mail server warning

not ping the server to send an email (single)

vim /root/scipt/1.sh

! # / bin / the bash 
of ping 192.168.0.116. 4 -C &> / dev / null packets. 4 #ping not displayed in the output terminal 
code = $? 
IF [ "$ code" -ne "0"]; each of the then # after the successful execution of the command will return $ = 0, -ne not equal? 
DATE = $ (DATE -d "Today" + "% Y-% M-% D_% H:% M:% S") # time format for the "Year - month - day _: minutes: seconds" 
echo "Server Machine iS 116 Down $ {dATE}" | / bin / mail -s "Server 192.168.0.116" 18210*****@139.com # echo part of the warning message content, / bin / mail call local mail service, -s followed by message subject, and finally mailbox 
fi

  

Note: There is a 139-mail e-mail message notification function in real-time, free of charge, to the function is on, when the message is received the phone message content can be displayed in real time

 

ping nowhere send an email server (multiple servers, IP connected)

vim /root/scipt/2.sh

#!/bin/bash
for i in $(seq 111 121) 或 for (i=111,i<121,i++) #这一句两种写法
do
ping -c 4 112.xxx.xxx.$i &> /dev/null
code=$?
if [ "$code" -ne "0" ];then
date=$(date -d "today" +"%Y-%m-%d_%H:%M:%S")
echo "112.xxx.xxx.$i Server machine is down ${date}" | /bin/mail -s "Server 112.xxx.xxx.$i" 18210*****@139.com #$i变量会自动切换
fi
done

  

ping nowhere website to send a message

vim /root/scipt/3.sh

#!/bin/bash
ping -c 4 www.abc.com > /dev/null
code=$?
if [ "$code" -ne "0" ];then
echo "Destination abc Unreachable" | /bin/mail -s "abc web" 18210*****@139.com
fi

  

Finally, plan to be a task, let it be performed once every 5 minutes

#crontab -e #crontab direct overwrite the original mission plan, and crontab -e to be increased based on the original

*/5 * * * * /root/script/1.sh

*/5 * * * * /root/script/2.sh

*/5 * * * * /root/script/3.sh

# "* * * * * - minutes, small, sun, moon, stars," * / 5 on behalf of every 5 minutes


What if one network segment to ping it

#!/bin/bash

for ((i=1;<255;i++))
do
ping -c 1 192.186.1.$i
done

  

=======================================================

Guess you like

Origin www.cnblogs.com/eos666/p/11828653.html