#!/bin/bash #Coded by:helloworld #Date:2022-2-20 10:00 #Version:2.0 ####################################################################################### #Set the script variable # IP_DEVNAME_LIST=ip_devname_list.txt # ####################################################################################### #Send a alert mail to the specific mailbox # send_alert_email() { ( echo "From:"; echo "To: "; echo "Subject:Alert_email"; echo ""; echo "$LINE_COUNT IPCAM down:"; cat bad_ip_devname.txt; ) | ssmtp -v -f "root@OpenWrtSlave" -F "root@OpenWrtSlave" helloworld_first@qq.com } # ####################################################################################### #Check if the specific file exits or not # if [ ! -f $IP_DEVNAME_LIST ];then echo "File $IP_DEVNAME_LIST doesn"t exits!" exit fi # ####################################################################################### #Check if the specific file is empty or not # if [ ! -s $IP_DEVNAME_LIST ];then echo "File $IP_DEVNAME_LIST is empty!" exit fi # ####################################################################################### #Loop to check every ipcam is online or offline # cat $IP_DEVNAME_LIST | cut -d "," -f 1 >> IP_LIST for IP in `cat IP_LIST` do ping -c 1 -W 2 $IP &> /dev/null if [ $? -ne 0 ];then echo "$IP" >> down_ip.txt fi sleep 1 done if [ -f down_ip.txt ];then for IP in `cat down_ip.txt` do cat $IP_DEVNAME_LIST | grep $IP >> bad_ip_devname.txt done LINE_COUNT=`wc -l bad_ip_devname.txt | cut -d " " -f 1` send_alert_email else echo "all the IPCAM are working." fi # ##################################################################################### #Remove the temporary file # rm -rf IP_LIST rm -rf down_ip.txt rm -rf bad_ip_devname.txt