Network Monitoring

My Office Hours between 10 A.M to 5 P.M .I am managing 16 client PCs which is remotely placed. I want to know the network status of every client PCs.I need an automatic trigger mail ,when the network connection is lost in any one of the Client PCs during office hours.I am a self study learner.I don't have an idea to develop the shell script.So Please give me a shell script. Please help me out of this problem.

mail id : upload.XXX@example.com.
   

Why use a mail? Why not ping them from your machine?

Because Alert Mail concept is useful to monitor the network connections when the network connection is lost and I have to take necessary steps to solve this issue.Already I had developed a shell Script a for network monitoring about the current network status.But I want the Network status through one mail when the network connection is lost.

mail eg:
If the 10th client PC Network Connection is lost,then I want to get a mail like as,
Please check the network connection of this 10th client PC. 

Help me out - how do you plan to deliver that mail once the connection is lost?

Actually the plan is ,I want to ping 16 IP address and get the status through mail once when the connection is lost.I have mentioned the 16 IP addresses as follows,

10.66.1.133  
10.66.6.133  
10.66.7.133   
10.66.0.133 
10.66.1.5     
10.66.2.133  
10.66.4.133  
10.66.3.133   
10.66.5.133   
10.66.2.5    
10.66.3.5    
10.66.7.5    
10.66.0.5    
10.66.6.5    
10.66.4.5    
10.66.5.5

I had developed a Network Monitoring script which is useful to monitoring the network and display the current network status of up and down.But I want to know the Network status through mail once when the connection is lost in any one of the 16 client PCs during office hours( 10.00 A.M to 5.00 P.M).My developed script is as follows, Can u develop the below script for Network monitoring through mail concept.I need a mail alert when the network connection is lost.Please help me out.

# My Developed script:
#!/bin/bash

ip=0

FilePath="/root/BranchIPMonitoring.txt"

UplistFilePath="/root/Uplist.txt"

DownlistFilePath="/root/Downlist.txt"

rm -rf $FilePath

rm -rf $UplistFilePath

rm -rf $DownlistFilePath

First_IPADDR()

{

            #####753-TIRUNELVELIUNITI-10.66.1.133#####

ip=10.66.1.133                     

branchname="TIRUNELVELI UNIT I"

branchcode=753

if ping -c 1 -W 1 $ip > /dev/null

then

echo "$branchcode $branchname of IPaddress $ip is up " >> $FilePath

else

echo "$branchcode $branchname of IPaddress $ip is down " >> $FilePath

fi

}

Second_IPADDR()

{

            #####74P-TIRUNELVELIUNITII-10.66.6.133#####

ip=10.66.6.133

branchname="TIRUNELVELI UNIT II"

branchcode=74P

if ping -c 1 -W 1 $ip > /dev/null

then

echo "$branchcode $branchname of IPaddress $ip is up " >> $FilePath

else

echo "$branchcode $branchname of IPaddress $ip is down " >> $FilePath

fi

}

Third_IPADDR()

{

            #####74U-TIRUNELVELICAB-10.66.7.133#####  

ip=10.66.7.133

branchname="TIRUNELVELI CAB"

branchcode=74U

if ping -c 1 -W 1 $ip > /dev/null

then

echo "$branchcode $branchname of IPaddress $ip is up " >> $FilePath

else

echo "$branchcode $branchname of IPaddress $ip is down " >> $FilePath

fi

}

Fourth_IPADDR()

{

            #####666-TENKASI-10.66.0.133#####

ip=10.66.0.133

branchname="TENKASI"

branchcode=666

if ping -c 1 -W 1 $ip > /dev/null

then

echo "$branchcode $branchname of IPaddress $ip is up " >> $FilePath

else

echo "$branchcode $branchname of IPaddress $ip is down " >> $FilePath

fi

}

Fifth_IPADDR()

{

            #####667-KOVILPATTI-10.66.1.5#####

ip=10.66.1.5

branchname="KOVILPATTI"

branchcode=667

if ping -c 1 -W 1 $ip > /dev/null

then

echo "$branchcode $branchname of IPaddress $ip is up " >> $FilePath

else

echo "$branchcode $branchname of IPaddress $ip is down " >> $FilePath

fi

}

Sixth_IPADDR()

{

            #####755-TUTICORIN-10.66.2.133#####

ip=10.66.2.133

branchname="TUTICORIN"

branchcode=755

if ping -c 1 -W 1 $ip > /dev/null

then

echo "$branchcode $branchname of IPaddress $ip is up " >> $FilePath

else

echo "$branchcode $branchname of IPaddress $ip is down " >> $FilePath

fi

}

Seventh_IPADDR()

{

            #####74B-THIRUCHENDURE-10.66.4.133#####

ip=10.66.4.133

branchname="THIRUCHENDURE"

branchcode=74B

if ping -c 1 -W 1 $ip > /dev/null

then

echo "$branchcode $branchname of IPaddress $ip is up " >> $FilePath

else

echo "$branchcode $branchname of IPaddress $ip is down " >> $FilePath

fi

}

Eighth_IPADDR()

{

            #####32A-SRIVAIKUNDAM-10.66.3.133#####

ip=10.66.3.133

branchname="SRIVAIKUNDAM"

branchcode=32A

if ping -c 1 -W 1 $ip > /dev/null

then

echo "$branchcode $branchname of IPaddress $ip is up " >> $FilePath

else

echo "$branchcode $branchname of IPaddress $ip is down " >> $FilePath

fi

}

Nineth_IPADDR()

{

            #####74G-VALLIOOR-10.66.5.133#####

ip=10.66.5.133

branchname="VALLIOOR"

branchcode=74G

if ping -c 1 -W 1 $ip > /dev/null

then

echo "$branchcode $branchname of IPaddress $ip is up " >> $FilePath

else

echo "$branchcode $branchname of IPaddress $ip is down " >> $FilePath

fi

}

Tenth_IPADDR()

{

            #####754-NAGERCOILUNITI-10.66.2.5#####

ip=10.66.2.5

branchname="NAGERCOIL UNIT I"

branchcode=754

if ping -c 1 -W 1 $ip > /dev/null

then

echo "$branchcode $branchname of IPaddress $ip is up " >> $FilePath

else

echo "$branchcode $branchname of IPaddress $ip is down " >> $FilePath

fi

}

Eleventh_IPADDR()

{

            #####7251-NAGERCOILUNITII-10.66.3.5#####   

ip=10.66.3.5

branchname="NAGERCOIL UNIT II"

branchcode=7251

if ping -c 1 -W 1 $ip > /dev/null

then

echo "$branchcode $branchname of IPaddress $ip is up " >> $FilePath

else

echo "$branchcode $branchname of IPaddress $ip is down " >> $FilePath

fi

}

Twelveth_IPADDR()

{

            #####74S-THUCALAY-10.66.7.5#####

ip=10.66.7.5

branchname="THUCALAY"

branchcode=74S

if ping -c 1 -W 1 $ip > /dev/null

then

echo "$branchcode $branchname of IPaddress $ip is up " >> $FilePath

else

echo "$branchcode $branchname of IPaddress $ip is down " >> $FilePath

fi

}

Thirteenth_IPADDR()

{

            #####300-KUZHITHURAI-10.66.0.5#####   

ip=10.66.0.5

branchname="KUZHITHURAI"

branchcode=300

if ping -c 1 -W 1 $ip > /dev/null

then

echo "$branchcode $branchname of IPaddress $ip is up " >> $FilePath

else

echo "$branchcode $branchname of IPaddress $ip is down " >> $FilePath

fi

}

Fourteeenth_IPADDR()

{

            #####74H-SANKARANKOIL-10.66.6.5#####

ip=10.66.6.5

branchname="SANKARANKOIL"

branchcode=74H

if ping -c 1 -W 1 $ip > /dev/null

then

echo "$branchcode $branchname of IPaddress $ip is up " >> $FilePath

else

echo "$branchcode $branchname of IPaddress $ip is down " >> $FilePath

fi

}

Fifteenth_IPADDR()

{

            #####32B-CHERANMAHADEVI-10.66.4.5#####

ip=10.66.4.5

branchname="CHERAN MAHADEVI"

branchcode=32B

if ping -c 1 -W 1 $ip > /dev/null

then

echo "$branchcode $branchname of IPaddress $ip is up " >> $FilePath

else

echo "$branchcode $branchname of IPaddress $ip is down " >> $FilePath

fi

}

Sixteenth_IPADDR()

{                                       

            #####74E-AMBAI-10.66.5.5#####

ip=10.66.5.5   

branchname="AMBAI"

branchcode=74E

if ping -c 1 -W 1 $ip > /dev/null

then

echo "$branchcode $branchname of IPaddress $ip is up " >> $FilePath

else

echo "$branchcode $branchname of IPaddress $ip is down " >> $FilePath

fi

}

First_IPADDR

Second_IPADDR

Third_IPADDR

Fourth_IPADDR

Fifth_IPADDR

Sixth_IPADDR

Seventh_IPADDR

Eighth_IPADDR

Nineth_IPADDR

Tenth_IPADDR

Eleventh_IPADDR

Twelveth_IPADDR

Thirteenth_IPADDR

Fourteeenth_IPADDR

Fifteenth_IPADDR

Sixteenth_IPADDR

while :

do

clear

echo "  ********************************************************************"

echo "**            1. How many branch IP connection is up and those details    **"

echo "**            2. How many branch IP connection is down and those details  **"

echo "**            3. Exit                                                     **"

echo "  ********************************************************************"

echo " "

echo -n "Please select your choice [ 1 to 3 ] :: "

read choice

case "$choice" in

1) cat $FilePath | grep up > $UplistFilePath

   uplistcount=`wc -l $UplistFilePath | cut -c1-2`

   echo " "

   echo " $uplistcount number of Branch Network connections are working fine  "

   echo " "

   cat $FilePath | grep up

   echo " "

   echo "Press [enter] key to continue. . ."

   read enterKey;; 

2) cat $FilePath | grep down > $DownlistFilePath

   downlistcount=`wc -l $DownlistFilePath | cut -c1-2`

   echo " "

   echo " $downlistcount number of Branch Network connections are down "

   echo " "

   if  cat $FilePath | grep down

   then   echo " "

   else

   echo " No branch is down "

   echo " "

   fi

   echo "Press [enter] key to continue. . ."

   read enterKey;;

3) echo "Bye $USER"

   exit 1;;

     *) echo "$choice is an invaild option. Please select option between 1-3 only"

     echo "Press [Enter] key to continue. . ."

     read enterKey;;

esac

done

With a TAB delimited file IPfile.txt like

10.66.1.133     TIRUNELVELI UNIT I      753
10.66.6.133     TIRUNELVELI UNIT II     74P
10.66.7.133     TIRUNELVELI CAB 74U
10.66.0.133     TENKASI 666
10.66.1.5       KOVILPATTI      667
10.66.2.133     TUTICORIN       755
10.66.4.133     THIRUCHENDURE   74B
10.66.3.133     SRIVAIKUNDAM    32A
10.66.5.133     VALLIOOR        74G
10.66.2.5       NAGERCOIL UNIT I        754
10.66.3.5       NAGERCOIL UNIT II       7251
10.66.7.5       THUCALAY        74S
10.66.0.5       KUZHITHURAI     300
10.66.6.5       SANKARANKOIL    74H
10.66.4.5       CHERAN MAHADEVI 32B
10.66.5.5       AMBAI   74E

, you could try

#!/bin/bash
UplistFilePath="/root/Uplist.txt"
DownlistFilePath="/root/Downlist.txt"
rm -rf $UplistFilePath
rm -rf $DownlistFilePath

while IFS="\t" read   $ip $branchname $branchcode
        do      ping -c 1 -W 1 $ip > /dev/null && { RES=up; FN=$UplistFilePath; } || { RES=down; FN=$DownlistFilePath; }
                printf "%s %s of IPaddress %s is %s.", $branchcode, $branchname, $ip, $RES > "$FN"
        done < IPfile.txt

uplistcount=$(wc -l < $UplistFilePath)
downlistcount=$(wc -l < $DownlistFilePath)


while :
        do clear

           select CH in "How many branch IP connection is up and those details" \
                        "How many branch IP connection is down and those details" \
                        "Exit"
                do case $REPLY in
                        1) echo " $uplistcount number of Branch Network connections are working fine  "
                           echo " "
                           cat $UplistFilePath ;;

                        2) echo " $downlistcount number of Branch Network connections are down "
                           echo " "
                           [ "$downlistcount" -eq 0 ] &&  echo " No branch is down " || cat  $DownlistFilePath ;;

                        3) echo "Bye $USER"
                           exit 1;;

                        *) echo "$REPLY is an invaild option. Please select option between 1-3 only" ;;
                   esac
                done

                echo " "
                echo "Press [enter] key to continue. . ."
                read enterKey

        done

Replace the second while loop with e.g.

mail -s "IPs down" upload.XXX@example.com < $DownlistFilePath

to receive a mail with all down IPs.

1 Like

First of all thanks to rudic for putting great effort to develop a script,
I think you are a genious

But I want to ping ip continuously from 10.00 A.M to 5.00 P.M, ( time specifications is important or else shell script continuously checking ip address to get a automatic triggering mail when network connection is lost)

When I am executing a shell script , I want to ping IP address morning 10.00 A.M to 5.00 P.M continuously,I want to get a mail once when the network connection is lost( for 16 IP address).( checking process continuously)

when I am executing a shell script ,I don't want to get the output from a terminal.But I just want to get a mail once when network connection is lost.This checking process is between 10 A.M to 5.00 P.M (continuous process)

sample example code for One IP Address :
 
if    ping $ip > /dev/null 

 then

echo " " > /dev/null

else 

  echo " $branchcode-$branchname-$ip is down " >> mailcontent
 
 mail -s " IPs down " upload.XXX@example.com < mailcontent

fi

Use cron to run that script every n minutes from 10:00h till 17:00h. If you replace the second while loop with a mail command there won't be any terminal output (which cron would send per mail anyhow, so you could skip mailing in your script...)

1 Like

Can u please replace the mail concept in while loop and please mention the cron entries in crontab.

I don't know How can I replace the mail concept in while loop and cron entries in crontab too.Please modify the shell script for mail concept in while loop and please mention the cron entries in crontab for 10.00 A.M to 5.00 P.M.Please give me a complete shell script for mail concept and cron entries too.

Actually I want to ping continuously and then I want to get a mail when the connection is lost.If Network cuts down in any 16 ip that time only I want to get a one mail ip is down.

Well, try this (untested!):

#!/bin/bash
UplistFilePath="/root/Uplist.txt"
DownlistFilePath="/root/Downlist.txt"
rm -rf $UplistFilePath
rm -rf $DownlistFilePath

while IFS="\t" read   $ip $branchname $branchcode
        do      ping -c 1 -W 1 $ip > /dev/null && { RES=up; FN=$UplistFilePath; } || { RES=down; FN=$DownlistFilePath; }
                printf "%s %s of IPaddress %s is %s.", $branchcode, $branchname, $ip, $RES > "$FN"
        done < IPfile.txt
[ -s "$DownlistFilePath" ] &&  cat "$DownlistFilePath" 

use a crontab entry like

MAILTO=upload.XXX@example.com
*/5 10-17 * * 1-5 yourpath/yourscript

Your crontab may need a different syntax, though. And, all of this is not tested, so please read man pages and try to eradicate errors yourself...

1 Like