forked from fire1ce/UDM-Failover-Telegram-Notifications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfailover-notifications.sh
executable file
·97 lines (86 loc) · 3.17 KB
/
failover-notifications.sh
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
#!/bin/sh
### Config ###
# Telegram Bot Token
telegram_bot_API_Token="YOUR_TELEGRAM_BOT_API_TOKEN"
# Telegram Chat ID
telegram_chat_id="YOUR_TELEGRAM_CHAT_ID"
# Destenation Host IP
echo_server_ip="1.1.1.1"
# Interval between checks (in seconds)
run_interval=60
### End Config ###
# funtion to return the current WAN interface name
get_current_wan_interface_name() {
local current_wan_interface_name
current_wan_interface_name=$(ip route get $echo_server_ip | grep -o "dev.*" | awk '/dev/ { print $2}')
if [ $current_wan_interface_name ]; then
echo $current_wan_interface_name
else
echo "None"
fi
}
get_current_wan_interface_external_ip() {
current_wan_interface_ip=$(curl -s -X GET https://checkip.amazonaws.com --max-time 10)
if [ $current_wan_interface_ip ]; then
echo $current_wan_interface_ip
else
echo "None"
fi
}
# Set initial interface name to the current WAN interface name
interface_name=$(get_current_wan_interface_name)
# Prints the current WAN interface name and ip address
start_message="Starting failover notifications. Current interface: $(get_current_wan_interface_name) with external IP: $(get_current_wan_interface_external_ip)"
echo $start_message
# Send the start message to Telegram
telegram_notification=$(
curl -s -X GET "https://api.telegram.org/bot${telegram_bot_API_Token}/sendMessage?chat_id=${telegram_chat_id}" \
--data-urlencode "text=$start_message"
)
# if [[ ${telegram_notification=} == *"\"ok\":false"* ]]; then
# echo "Error! Telegram notification failed"
# echo ${telegram_notification=}
# fi
case "$telegram_notification" in
*"\"ok\":false"*)
echo "Error! Telegram notification failed"
echo ${telegram_notification}
;;
*) ;;
esac
# Loop to check if the current WAN interface name has changed
while true; do
current_wan_interface_name=$(get_current_wan_interface_name)
# if the current wan interface name is none then exit this loop and start over
if [ "$current_wan_interface_name" = "None" ]; then
echo "Error! Network is unreachable"
interface_name=$current_wan_interface_name
sleep $run_interval
continue
fi
# if the current WAN interface name is not the same as the initial interface name, then send a message to the Telegram chat
if [ "$current_wan_interface_name" != "$interface_name" ]; then
current_wan_interface_external_ip=$(get_current_wan_interface_external_ip)
# if the current WAN external ip is none then exit this loop and start over
if [ $current_wan_interface_external_ip == "None" ]; then
echo "Can't get external IP"
sleep $run_interval
continue
fi
message="Failover: WAN interface changed from $interface_name to: $current_wan_interface_name with external IP: $current_wan_interface_external_ip"
echo $message
telegram_notification=$(
curl -s -X GET "https://api.telegram.org/bot${telegram_bot_API_Token}/sendMessage?chat_id=${telegram_chat_id}" \
--data-urlencode "text=$message"
)
case "$telegram_notification" in
*"\"ok\":false"*)
echo "Error! Telegram notification failed"
echo ${telegram_notification}
;;
*) ;;
esac
interface_name=$current_wan_interface_name
fi
sleep $run_interval
done