-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouterrd
More file actions
117 lines (94 loc) · 3.75 KB
/
routerrd
File metadata and controls
117 lines (94 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
#Title........: Router Redundancy Detector
#Description..: program to email to the network admin if a router fails
#Author.......: billy-paul1234
#Usage........: bash routerrd
if [ $# -eq 4 -o $# -eq 2 ]
then
timeinterval=9999
router1=$(ping $1 -q -c 3 | grep loss | awk '{print $4}')
router2=$(ping $2 -q -c 3 | grep loss | awk '{print $4}')
[ $# -eq 2 ] && mailtime=9999 || mailtime=$3
timeformat="+%$4"
mailid=$(cat /etc/ssmtp/ssmtp.conf | grep AuthUser);mailid=${mailid:9}
tmp="/var/routerrd/mail-sent-time.txt"
#echo $mailid
#if [ [ "$router1" == "0" || "$router2" == "0" && $# -ne 2 ] ]
[ $# -ne 2 ] && if [ "$router1" == '0' -o "$router2" == '0' ]
then
currenttime=$(( $( date $timeformat | sed 's/^0*//' )))
mailsenttime=$(( $( cat $tmp | sed 's/^0*//' )))
[ $mailsenttime -eq 9999 ] && timeinterval=9999 || timeinterval=$(($currenttime - $mailsenttime))
[ $timeinterval -lt 0 ] && timeinterval=$((($mailsenttime + $timeinterval) + (60 - $mailsenttime)))
#echo currenttime = $currenttime
#echo mailsenttime = $mailsenttime
#echo timeinterval = $timeinterval
fi
#echo "------------------------"
#echo $router1 "<--router 1"
#echo $router2 "<--Router 2"
#echo --------------------------
#This condition works, when router 1 is DOWN and router 2 is UP.
if [ "$router1" == "0" -a "$router2" != "0" ]
then
[ $timeinterval -ge $mailtime -o $timeinterval -eq 9999 ] && if [ "$mailid" == "" ]
then
echo "Please setup your mailserver"
else
echo "Router 1 is DOWN. Now router 2 is UP. Date & Time: $(date)" | mail -s "RouterRD" $mailid
#echo "Sending mail.."
[ $# -ne 2 ] && date $timeformat > $tmp
fi
#This condition works, when Two router is also DOWN
elif [ "$router2" == "0" -a "$router1" == "0" ]
then
[ $timeinterval -ge $mailtime -o $timeinterval -eq 9999 ] && if [ "$mailid" == "" ]
then
echo "Please Setup your mailserver"
else
echo "Router 2 is also DOWN. There is no Internet connection. Date & Time: $(date)" | mail -s "RouterRD" $mailid
#echo "Sending mail.."
[ $# -ne 2 ] && date $timeformat > $tmp
fi
# This condition works, when router 1 is UP and router 2 is DOWN
elif [ "$router2" == "0" -a "$router1" != "0" ]
then
[ $timeinterval -ge $mailtime -o $timeinterval -eq 9999 ] && if [ "$mailid" == "" ]
then
echo 'Please setup your mailserver'
else
#echo "Sending mail..."
echo "Router 2 is DOWN but Router 1 is UP. Date & Time: $(date)" | mail -s "RouterRD" $mailid
[ $# -ne 2 ] && date $timeformat > $tmp
fi
#This Works when two router is also UP
else
#echo "Router is Not chandged"
[ $# -ne 2 ] && echo 9999 > $tmp
fi
else
echo
echo "
routerrd --> RRD(Router Redundency Detector)
routerrd --> command is used to detect the router state which uses HSRP(Hot Standby Routing Protocol) and send mail if any router is down
Usage:
routerrd [ R1-ip ] [ R2-ip ]
Example:
routerrd 192.168.1.2 192.168.1.3
R1-ip --> Router 1 ip address
R2-ip --> Router 2 ip address
If you want to check your router state for each minute, add the line like bellow example by typing the command ( sudo crontab -e )
Don't forget to customize your ip and time
Syntax:
* * * * * routerrd [ R1-ip ] [ R2-ip ] [ TI ] [ 'M' or 'S' ]
Example:
* * * * * routerrd 192.168.1.2 192.168.1.3 20 'M'
* * * * * routerrd 192.168.1.2 192.168.1.3 50 'S'
TI --> Time interval to send mail if the router is still down
For Minute use ( 2 - 58 )
For Seconds Use ( 10 - 50 )
'M' --> Time in minutes
'S' --> Time is seconds
routerrd -h --> to display this discription
"
fi