-
Notifications
You must be signed in to change notification settings - Fork 0
/
bm-hotspot-cli.sh
executable file
·357 lines (327 loc) · 12.7 KB
/
bm-hotspot-cli.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/env bash
# Manage Talkgroups on MMDVM-Hotspot via Brandmeister-API
# The BM API had changed after this was orignally created.
# This is just an attempt to modernize it.
clear
# Config and variables have been broken off into ONE file
source bm-hotspot-cli.conf
ver="3.1"
function banner {
echo $(tput setaf 3)
echo " ___ __ __ ___ _ ___ ";
echo " | _ ) \/ |___ / __| | |_ _| ";
echo " | _ \ |\/| |___| (__| |__ | | ";
echo " |___/_| |_| \___|____|___| ";
echo " $(tput sgr0)v$ver";
}
function dep_check {
curlinstalled=$(which curl)
if [[ "$?" == 1 ]]; then
printf "\n\n This Script requires curl.\n Please install 'curl' to continue.\n\n\n"
exit 0
fi
jqinstalled=$(which jq)
if [[ "$?" == 1 ]]; then
printf "\n\n This Script requires jq.\n Please install 'jq' to continue.\n\n\n"
exit 0
fi
}
function check_apikey {
if [[ -z "$APIKEY" ]]; then
printf "\n\n $(tput bold)API-Key Not Found.$(tput sgr0)\n"
printf " Enter Brandmeister API-KEY in $(tput bold)bm-hotspot-cli.conf$(tput sgr0)!\n\n\n"
exit 0
fi
}
function check_hotspotid {
if [[ -z "$HOTSPOT" ]]; then
printf "\n\n $(tput bold)Hotspot ID Not Found.$(tput sgr0)\n"
printf " Enter Hotspot ID in $(tput bold)bm-hotspot-cli.conf$(tput sgr0)!\n\n\n"
exit 0
fi
}
function showsettings {
printf "\n Hotspot ID: $HOTSPOT ($CALLSIGN)\n"
}
function refresh_cache {
printf "\n Caching hotspot information for $HOTSPOT.\n"
curl -s $APIURL/$HOTSPOT/talkgroup -H "accept: application/json" > ./bm-cli.json
curl -X 'GET' -s "$APIURL/$HOTSPOT" -H 'accept: application/json' > hotspot-info.json
CALLSIGN=$(jq '.callsign' 'hotspot-info.json' | sed 's/\"//g')
}
function menu {
banner
showsettings
printf "\n$(tput bold) BRANDMEISTER HOTSPOT CONTROL$(tput sgr0)\n"
printf "$(tput bold) ---------------------------------------$(tput sgr0)\n"
printf " [$(tput bold)1$(tput sgr0)] Show current Dynamic and Static TGs\n"
printf " [$(tput bold)2$(tput sgr0)] Drop current QSO\n"
printf " [$(tput bold)3$(tput sgr0)] Drop ALL Dynamic TGs\n"
printf " [$(tput bold)4$(tput sgr0)] Add Static TG\n"
printf " [$(tput bold)5$(tput sgr0)] Drop Static TG\n"
printf " [$(tput bold)6$(tput sgr0)] Drop ALL Static TGs\n"
printf " [$(tput bold)7$(tput sgr0)] Show hotspot information\n"
printf " [$(tput bold)A$(tput sgr0)] About\n"
printf " [$(tput bold)R$(tput sgr0)] Refresh Cache\n"
printf " [$(tput bold)Q$(tput sgr0)] Quit\n"
printf " $(tput bold)---------------------------------------$(tput sgr0)\n\n"
read -r -sn1 menu_selection
case "$menu_selection" in
[1]) show_tgs;;
[2]) drop_qso;;
[3]) drop_dynamic_tgs;;
[4]) add_static_tg;;
[5]) drop_static_tg;;
[6]) drop_all_static_tgs;;
[7]) show_hotspot;;
[0]) bunny;;
[aA]) about;;
[rR]) refresh;;
[qQ]) printf "\n"; rm *.json; exit;;
esac
}
function show_tgs {
# Confirmed that the code
printf "$(tput sgr0)$(tput setaf 3)Inquire current TGs...$(tput sgr0)\n"
#curl -s $APIURL/$HOTSPOT/talkgroup -H "accept: application/json" > ./bm-cli.json
printf "$(tput bold)\n Static TS1: $(tput sgr0)$(tput setaf 6)"
jq '.[] | select(.slot=="1").talkgroup' 'bm-cli.json' | tr '\n' ' '
printf "$(tput sgr0)"
printf "$(tput bold)\n Static TS2: $(tput sgr0)$(tput setaf 6)"
jq '.[] | select(.slot=="2").talkgroup' 'bm-cli.json' | tr '\n' ' '
printf "$(tput sgr0)\n"
grep -q dynamicSubscriptions ./bm-cli.json
if [ $? = 1 ];then
printf "$(tput bold)\n Dynamic: $(tput sgr0)$(tput setaf 6)NONE LISTED$(tput sgr0)"
else
printf "$(tput bold)\n Dynamic on TS1: $(tput sgr0)$(tput setaf 6)"
jq '.dynamicSubscriptions[] | select(.slot=="1").talkgroup' './bm-cli.json' | tr '\n' ' ' # The data is pulled correctly now
printf "$(tput sgr0)\n"
printf "$(tput bold)\n Dynamic on TS2: $(tput sgr0)$(tput setaf 6)"
jq '.dynamicSubscriptions[] | select(.slot=="2").talkgroup' './bm-cli.json' | tr '\n' ' ' # The data is pulled correctly now
printf "$(tput sgr0)\n"
fi
grep -q timedSubscriptions ./bm-cli.json
if [ $? = 1 ];then
printf "$(tput bold)\n Timed Static: $(tput sgr0)$(tput setaf 6)NONE LISTED$(tput sgr0)"
else
printf "$(tput bold)\n Timed Static on TS1: $(tput sgr0)$(tput setaf 6)"
jq '.timedSubscriptions[] | select(.slot=="1").talkgroup' './bm-cli.json' | tr '\n' ' ' # Need to verify this works still
printf "$(tput sgr0)\n"
printf "$(tput bold)\n Timed Static on TS2: $(tput sgr0)$(tput setaf 6)"
jq '.timedSubscriptions[] | select(.slot=="2").talkgroup' './bm-cli.json' | tr '\n' ' ' # Need to verify this works still
printf "$(tput sgr0)\n"
fi
printf "\n"
menu
}
function drop_qso {
ts=""
printf "\n"
while [[ ! $ts =~ ^[1-2] ]]; do
printf " Enter the Timeslot (Numbers Only): "
read ts
done
printf "$(tput setaf 3)Dropping current QSOs on Timeslot $ts...$(tput sgr0)\n\n "
curl -X 'GET' -s "$APIURL/$HOTSPOT/action/dropCallRoute/$ts" \
-H 'accept: application/json' \
-H "Authorization: Bearer $APIKEY"
printf "\n"
menu
}
function drop_dynamic_tgs {
ts=""
printf " Enter the timeslot to drop ALL dynamic talkgroups from: "
read ts
printf "$(tput setaf 3)Dropping Dynamic Talkgroups from Timeslot $ts...$(tput sgr0)\n\n "
curl -X 'GET' -s "$APIURL/$HOTSPOT/action/dropDynamicGroups/$ts" \
-H 'accept: application/json' \
-H "Authorization: Bearer $APIKEY" | jq '.message'
menu
}
function add_static_tg {
tg=""
ts=""
printf "\n"
while [[ ! $ts =~ ^[1-2] ]]; do
printf " Enter the Timeslot (Numbers Only): "
read ts
done
while [[ ! $tg =~ ^[0-9] ]]; do
printf " Enter Talkgroup ID (Numbers Only): "
read tg
done
printf "\n$(tput setaf 3) Adding Talkgroup $tg to Timeslot $ts.$(tput sgr0)\n\n "
curl -X 'POST' -s \
"$APIURL/$HOTSPOT/talkgroup" \
-H 'accept: application/json' \
-H "Authorization: Bearer $APIKEY" \
-H 'Content-Type: application/json' \
-d "{\"slot\": $ts,\"group\": $tg}" | jq '.talkgroup' | grep -q "$tg"
if [ $? = 0 ];then
printf " Talkgroup $tg was added successfully to Timeslot $ts!\n"
else
printf " Error! TG $tg was $(tput bold)NOT$(tput sgr0) added to Timeslot $ts!\n"
fi
printf "\n$(tput setaf 3) Updating cache...\n\n "
curl -s $APIURL/$HOTSPOT/talkgroup -H "accept: application/json" > ./bm-cli.json
show_tgs
menu
}
function drop_static_tg {
tg=""
ts=""
printf "\n"
while [[ ! $ts =~ ^[1-2] ]];do
printf " Enter Timeslot (Numbers Only:) "
read ts
done
while [[ ! $tg =~ ^[0-9] ]]; do
printf " Enter Talkgroup ID (Numbers Only): "
read tg
done
printf "\n$(tput setaf 3) Dropping Talkgroup $tg from Timeslot $ts...$(tput sgr0)\n\n "
curl -X 'DELETE' -s -o output "$APIURL/$HOTSPOT/talkgroup/$ts/$tg" \
-H 'accept: */*' \
-H "Authorization: Bearer $APIKEY" | jq '.message'
grep -q "1" output
if [ $? = 0 ];then
printf " Talkgroup $tg has been $(tput bold)removed$(tput sgr0) from Timeslot $ts.\n"
rm output
printf "\n$(tput setaf 3) Updating cache...\n\n "
curl -s $APIURL/$HOTSPOT/talkgroup -H "accept: application/json" > ./bm-cli.json
show_tgs
menu
else
printf "$(tput bold) There was some kind of error and Talkgroup $tg was NOT removed from Timeslot $ts!$(tput sgr0)\n"
fi
rm output
menu
}
function drop_all_static_tgs {
# curl current static TGs into an array
ts=""
ans=""
curl -s $APIURL/$HOTSPOT/talkgroup -H "accept: application/json" > ./drop-bm-cli.json
while [[ ! $ts =~ ^[1-2] ]]; do
printf " Enter the Timeslot (Numbers Only): "
read -r -sn1 ts
done
case "$ts" in
1)
printf "\n Selecting talkgroup $ts\n"
jq '.[] | select(.slot=="1").talkgroup' './drop-bm-cli.json' | tr '\n' ',' > to-nuke
sed -i 's/\"//g' to-nuke
sed -i 's/.$//' to-nuke
printf " Are you $(tput bold)SURE$(tput sgr0) (Y/N)?\n"
read -r -sn1 ans
case "$ans" in
Y|y);;
N|n)
menu
;;
esac
printf " Are you $(tput bold)REALLY SURE$(tput sgr0) (Y/N)?\n"
read -r -sn1 ans
case "$ans" in
Y|y);;
N|n)
menu
;;
esac
;;
2)
printf "\n Selecting talkgroup $ts\n"
jq '.[] | select(.slot=="2").talkgroup' './drop-bm-cli.json' | tr '\n' ',' > to-nuke
sed -i 's/\"//g' to-nuke
cat to-nuke | tr ',' '\n' > really-nuke
printf " Are you $(tput bold)SURE$(tput sgr0) (Y/N)?\n"
read -r -sn1 ans
case "$ans" in
Y|y);;
N|n)
menu
;;
esac
printf " Are you $(tput bold)REALLY SURE$(tput sgr0) (Y/N)?\n"
read -r -sn1 ans
case "$ans" in
Y|y);;
N|n)
menu
;;
esac
;;
esac
static_tgs=$(cat to-nuke)
if [ -z $static_tgs ]; then
printf "\n No Static TGs on Hotspot $(tput bold)$HOTSPOT$(tput sgr0) $(tput bold)(TS $ts)$(tput sgr0) found, aborting...\n\n"
menu
else
IFS=''
while read i; do
curl -X 'DELETE' "$APIURL/$HOTSPOT/talkgroup/$ts/$i" \
-H 'accept: */*' \
-H "Authorization: Bearer $APIKEY"
printf " $(tput setaf 6)$i $(tput sgr0)Dropped\n\n"
done < really-nuke
fi
rm to-nuke
rm really-nuke
rm drop-bm-cli.json
printf "\n$(tput setaf 3) Updating cache...\n\n "
curl -s $APIURL/$HOTSPOT/talkgroup -H "accept: application/json" > ./bm-cli.json
show_tgs
menu
}
function show_hotspot {
#curl -X 'GET' -s "$APIURL/$HOTSPOT" -H 'accept: application/json' > hotspot-info.json
CALLSIGN=$(jq '.callsign' 'hotspot-info.json' | sed 's/\"//g')
HARDWARE=$(jq '.hardware' 'hotspot-info.json' | sed 's/\"//g')
TX=$(jq '.tx' 'hotspot-info.json' | sed 's/\"//g')
RX=$(jq '.rx' 'hotspot-info.json' | sed 's/\"//g')
CC=$(jq '.colorcode' 'hotspot-info.json' | sed 's/\"//g')
MASTER=$(jq '.lastKnownMaster' 'hotspot-info.json' | sed 's/\"//g')
STATUSTXT=$(jq '.statusText' 'hotspot-info.json' | sed 's/\"//g')
LASTSEEN=$(jq '.last_seen' 'hotspot-info.json' | sed 's/\"//g')
CREATED=$(jq '.created_at' 'hotspot-info.json' | sed 's/\"//g')
UPDATED=$(jq '.updated_at' 'hotspot-info.json' | sed 's/\"//g')
printf "$(tput sgr0)$(tput setaf 3)Inquire current hotspot settings...$(tput sgr0)\n"
printf "\n $(tput bold)Hotspot Information for $HOTSPOT ($CALLSIGN).$(tput sgr0)\n"
printf "\n $(tput bold)Last seen at $LASTSEEN.$(tput sgr0)\n"
printf "$(tput bold) -------------------------------------------$(tput sgr0)\n"
printf " $(tput bold)Hardware$(tput sgr0) [ $HARDWARE ]\n"
printf " $(tput bold)TX Freq$(tput sgr0) [ $TX ]\n"
printf " $(tput bold)RX Freq$(tput sgr0) [ $RX ]\n"
printf " $(tput bold)Color Code$(tput sgr0) [ $CC ]\n"
printf " $(tput bold)Server$(tput sgr0) [ $MASTER ]\n"
printf " $(tput bold)Status$(tput sgr0) [ $STATUSTXT ]\n"
printf " $(tput bold)Created On$(tput sgr0) [ $CREATED ]\n"
printf " $(tput bold)Updated Last$(tput sgr0) [ $UPDATED ]\n"
printf "$(tput bold) --------------------------------------------$(tput sgr0)\n"
printf "\n"
menu
}
function bunny {
printf "\n\n $(tput setaf 6)APIURL: $APIURL\n APIKEY: $APIKEY\n\n HOTSPOT_ID: $HOTSPOT\n\n"
dep_check
printf "\n\n $(tput setaf 5)/)___(\ \n (='.'=)\n (\")_(\")$(tput sgr0)$(tput bold) (v$ver) by $Author.$(tput sgr0)\n\n"
printf "\n\n $(tput bold)Modified by $MOD.$(tput sgr0)\n\n"
menu
}
function about {
clear
banner
printf "\n\nThis program was created to assist in managing the basics of your Brandmeister hotspot.\n"
printf "It was originally created in $Date by $Author for an earlier version of the API.\n"
printf "It was further modified by $MOD in $MODDate to use the current API (at that time).\n\n"
printf "$(tput bold)Press any key to return to menu.$(tput sgr0)"
read
clear
menu
}
dep_check
check_apikey
check_hotspotid
refresh_cache
menu