--The target IP is described in ʻip_node.list. --If there is any
packet loss`, you will be notified by email.
--The network settings and mail settings have been completed.
I will describe it as follows.
192.168.11.21
192.168.11.29
192.168.11.69
192.168.11.88
192.168.11.99
Like this.
bash
#!/bin/bash
LANG=C;
export LANG;
:> ~/mail.dat;
while read line;do
_RET_=`ping -c 5 -i 1 -w 10 -s 32 ${line} | grep 'loss' | grep -oP '[0-9]+%'`;
echo "${line} ${_RET_} loss";
[ "0%" == "${_RET_}" ] && continue;
echo "${line} ${_RET_} loss" >> ~/mail.dat;
done <~/ip_node.list;
_RET_=`du ~/mail.dat | awk '{print $1}' | xargs expr`;
[ ! 0 -eq ${_RET_} ] && mail -s "IP packet loss alert." -r [from addr] [to addr] < ~/mail.dat;
exit 0;
bash
#!/bin/bash
LANG=C;
export LANG; #When the Ping result is displayed in Japanese, the regular expression of grep is troublesome, so LANG=Fixed to C
:> ~/mail.dat; #Set the body for email notification to 0 bytes
while read line;do #Enter the file in the done part below$Variables for each line in line
_RET_=`ping -c 5 -i 1 -w 10 -s 32 ${line} | grep 'loss' | grep -oP '[0-9]+%'`; #Perform Ping, adjust options etc. to your liking, and get the matched part with gpre
echo "${line} ${_RET_} loss";
[ "0%" == "${_RET_}" ] && continue; # 0%Other than the notification, this is fine
echo "${line} ${_RET_} loss" >> ~/mail.dat; #Write for notification
done <~/ip_node.list;
_RET_=`du ~/mail.dat | awk '{print $1}' | xargs expr`; #Get the size of the file for notification
[ ! 0 -eq ${_RET_} ] && mail -s "IP packet loss alert." -r [from addr] [to addr] < ~/mail.dat; #If non-zero, notify
exit 0;
Recommended Posts