-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathupdate-notify.sh
More file actions
186 lines (151 loc) · 7.03 KB
/
update-notify.sh
File metadata and controls
186 lines (151 loc) · 7.03 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/sh
# Made by Jack'lul <jacklul.github.io>
#
# Send update notification
#
# Based on:
# https://github.com/RMerl/asuswrt-merlin.ng/wiki/Update-Notification-Example
#
#jas-update=update-notify.sh
#shellcheck shell=ash
#shellcheck disable=SC2155
#shellcheck source=./common.sh
readonly common_script="$(dirname "$0")/common.sh"
if [ -f "$common_script" ]; then . "$common_script"; else { echo "$common_script not found" >&2; exit 1; } fi
CRON="0 */6 * * *" # schedule as cron string
EMAIL_SMTP=""
EMAIL_PORT=""
EMAIL_USERNAME=""
EMAIL_PASSWORD=""
EMAIL_FROM_NAME=""
EMAIL_FROM_ADDRESS=""
EMAIL_TO_NAME=""
EMAIL_TO_ADDRESS=""
TELEGRAM_BOT_TOKEN=""
TELEGRAM_CHAT_ID=""
PUSHOVER_TOKEN=""
PUSHOVER_USERNAME=""
PUSHBULLET_TOKEN=""
CUSTOM_COMMAND="" # command will receive the new firmware version as its first parameter
load_script_config
state_file="$TMP_DIR/$script_name"
tmp_file="$TMP_DIR/$script_name.tmp"
router_ip="$(nvram get lan_ipaddr)"
router_name="$(nvram get lan_hostname)"
[ -z "$router_name" ] && router_name="$router_ip"
curl_binary="$(get_curl_binary)"
[ -z "$curl_binary" ] && { echo "curl not found"; exit 1; }
send_email_message() {
cat <<EOT > "$tmp_file"
From: "$EMAIL_FROM_NAME" <$EMAIL_FROM_ADDRESS>
To: "$EMAIL_TO_NAME" <$EMAIL_TO_ADDRESS>
Subject: New router firmware notification @ $router_name
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
New firmware version <b>$1</b> is now available for your router at <a href="$router_ip">$router_ip</a>.
EOT
$curl_binary --url "smtps://$EMAIL_SMTP:$EMAIL_PORT" --mail-from "$EMAIL_FROM_ADDRESS" --mail-rcpt "$EMAIL_TO_ADDRESS" --upload-file "$tmp_file" --ssl-reqd --user "$EMAIL_USERNAME:$EMAIL_PASSWORD" || logecho "Failed to send an email message" error
rm -f "$tmp_file"
}
send_telegram_message() {
local _linebreak=$(printf '\n\r')
local _message="<b>New router firmware notification @ $router_name</b>${_linebreak}${_linebreak}New firmware version <b>$1</b> is now available for your router at $router_ip."
local _result=$($curl_binary -fsS --data chat_id="$TELEGRAM_CHAT_ID" --data "protect_content=true" --data "disable_web_page_preview=true" --data "parse_mode=HTML" --data "text=$_message" "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage")
if ! echo "$_result" | grep -Fq '"ok":true'; then
if echo "$_result" | grep -Fq '"ok":'; then
logecho "Telegram API error: $_result" error
else
logecho "Connection to Telegram API failed: $_result" error
fi
fi
}
send_pushover_message() {
$curl_binary --form-string "token=$PUSHOVER_TOKEN" --form-string "user=$PUSHOVER_USERNAME" --form-string "title=New router firmware notification @ $router_name" --form-string "message=New firmware version $1 is now available for your router at $router_ip." "https://api.pushover.net/1/messages.json" || logecho "Failed to send Pushover message" error
}
send_pushbullet_message () {
$curl_binary -request POST --user "$PUSHBULLET_TOKEN": --header 'Content-Type: application/json' --data-binary '{"type": "note", "title": "'"New router firmware notification @ $router_name"'", "body": "'"New firmware version $1 is now available for your router at $router_ip."'"}' "https://api.pushbullet.com/v2/pushes" || logecho "Failed to send Pushbullet message" error
}
send_notification() {
[ -z "$1" ] && { echo "Version not passed" >&2; exit 1; }
if [ -n "$EMAIL_SMTP" ] && [ -n "$EMAIL_PORT" ] && [ -n "$EMAIL_USERNAME" ] && [ -n "$EMAIL_PASSWORD" ] && [ -n "$EMAIL_FROM_NAME" ] && [ -n "$EMAIL_FROM_ADDRESS" ] && [ -n "$EMAIL_TO_NAME" ] && [ -n "$EMAIL_TO_ADDRESS" ]; then
logecho "Sending update notification through Email..."
send_email_message "$1"
fi
if [ -n "$TELEGRAM_BOT_TOKEN" ] && [ -n "$TELEGRAM_CHAT_ID" ]; then
logecho "Sending update notification through Telegram..."
send_telegram_message "$1"
fi
if [ -n "$PUSHOVER_TOKEN" ] && [ -n "$PUSHOVER_USERNAME" ]; then
logecho "Sending update notification through Pushover..."
send_pushover_message "$1"
fi
if [ -n "$PUSHBULLET_TOKEN" ]; then
logecho "Sending update notification through Pushbullet..."
send_pushbullet_message "$1"
fi
if [ -n "$CUSTOM_COMMAND" ]; then
logecho "Sending update notification through custom command..."
$CUSTOM_COMMAND "$1"
fi
}
check_and_notify() {
{ [ "$(nvram get wan0_state_t)" != "2" ] && [ "$(nvram get wan1_state_t)" != "2" ] ; } && { echo "WAN network is not connected" >&2; exit 1; }
local _firmver=$(nvram get firmver | sed 's/[^0-9]//g')
local _buildno=$(nvram get buildno | sed 's/[^0-9]//g')
local _extendno=$(nvram get extendno | awk -F '-' '{print $1}' | sed 's/[^0-9]//g')
local _current_version="${_firmver}_${_buildno}_${_extendno}"
local _webs_state_info="$(nvram get webs_state_info)"
local _webs_firmver=$(echo "$_webs_state_info" | awk -F '_' '{print $1}' | sed 's/[^0-9]//g')
local _webs_buildno=$(echo "$_webs_state_info" | awk -F '_' '{print $2}' | sed 's/[^0-9]//g')
local _webs_extendno=$(echo "$_webs_state_info" | awk -F '_' '{print $3}' | awk -F '-' '{print $1}' | sed 's/[^0-9]//g')
if [ -z "$_firmver" ] || [ -z "$_buildno" ] || [ -z "$_extendno" ] || [ -z "$_webs_state_info" ]; then
echo "Could not gather valid values from NVRAM" >&2
exit 1
fi
if [ "$_firmver" -lt "$_webs_firmver" ] || [ "$_buildno" -lt "$_webs_buildno" ] || [ "$_extendno" -lt "$_webs_extendno" ]; then
if [ "$_current_version" != "$_webs_state_info" ] && { [ ! -f "$state_file" ] || [ "$(cat "$state_file")" != "$_webs_state_info" ] ; }; then
send_notification "$_webs_state_info"
echo "$_webs_state_info" > "$state_file"
fi
fi
}
case "$1" in
"run")
check_and_notify
;;
"test")
if { is_started_by_system && cru l | grep -Fq "#$script_name-test#"; } || [ "$2" = "now" ]; then
logecho "Testing notification..." alert
cru d "$script_name-test"
webs_state_info="$(nvram get webs_state_info)"
if [ -z "$webs_state_info" ]; then
firmver=$(nvram get firmver | sed 's/[^0-9]//g')
buildno=$(nvram get buildno | sed 's/[^0-9]//g')
extendno=$(nvram get extendno)
webs_state_info="${firmver}_${buildno}_${extendno}"
fi
if [ -n "$webs_state_info" ]; then
send_notification "$webs_state_info"
else
logecho "Unable to obtain current version info" error
fi
else
cru a "$script_name-test" "*/1 * * * * sh $script_path test"
echo "Scheduled a test with crontab, please wait one minute."
fi
;;
"start")
[ -n "$CRON" ] && crontab_entry add "$CRON $script_path run"
;;
"stop")
crontab_entry delete
;;
"restart")
sh "$script_path" stop
sh "$script_path" start
;;
*)
echo "Usage: $0 run|start|stop|restart|test"
exit 1
;;
esac