-
Notifications
You must be signed in to change notification settings - Fork 1
/
m900.sh
executable file
·242 lines (195 loc) · 5.59 KB
/
m900.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
#!/bin/bash
#--------------------------------------------------
# Script to refresh configuration for Snom M900
#
# you need to install sipsak
#
# Author: Walter Trucci - walter at trucci.it
# date: 30-10-2023
#--------------------------------------------------
# Ansi color code variables
red="\e[0;91m"
blue="\e[0;94m"
green="\e[0;92m"
bold="\e[1m"
uline="\e[4m"
reset="\e[0m"
usage() {
echo "Usage: $0 [ -d <m900 files folder> ] [ -h ]" 1>&2
echo "-d file folder: directory where the m900 files are stored" 1>&2
echo "-h: help" 1>&2
exit 1
}
text() {
local msg="$1"
echo -e "${msg}"
}
error() {
local msg="$1"
echo -e "${red}${msg}${reset}"
}
fail() {
local msg="$1"
echo -e "${red}${msg}${reset}" >&2
exit 1
}
# the create function add a file with m900 extension with parameter to send with sipsak
create() {
local name=""
local id=""
local m900Ip=""
local pbxIp=""
while :; do
text "Please give me a name ${bold}${uline}without spaces${reset}"
read name
if printf "%s" "$name" | grep -E '[ "]' 1>/dev/null; then
error "The name contains one or more spaces\n"
text "Please insert data again\n"
else
if [ $name ]; then
text "The name is $name\n"
break
else
error "The string can't be empty$\n"
text "Please insert data again\n"
fi
fi
done
while :; do
text "Please give me the multicell id"
read id
if [ -n "$id" ] && [ "$id" -eq "$id" ] 2>/dev/null; then
if ! ([ $id -ge 1 ] && [ $id -le 512 ]); then
error "BAD ID need to be in a range from 1 to 512\n"
text "Please insert data again\n"
else
break
fi
else
error "BAD ID need to be in a range from 1 to 512\n"
text "Please insert data again\n"
fi
done
while :; do
text "\nPlease give me the M900 IP"
read m900Ip
printf "%s" "${m900Ip}" | grep -Eo '^(([1-9]?[0-9]|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))\.){3}([1-9]?[0-9]|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))$' 1>/dev/null
if ! [ $? -eq 0 ]; then
error "BAD IP$\n"
text "Please insert data again\n"
else
break
fi
done
while :; do
text "\nPlease give me the PBX IP"
read pbxIp
printf "%s" "${pbxIp}" | grep -Eo '^(([1-9]?[0-9]|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))\.){3}([1-9]?[0-9]|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))$' 1>/dev/null
if ! [ $? -eq 0 ]; then
error "BAD IP\n"
text "Please insert data again\n"
else
break
fi
done
cat >"${DIRECTORY}${name}.m900" <<EOF
NOTIFY sip:$id@$m900Ip SIP/2.0
To: sip:$id@$m900Ip
From: sip:sipsak@$pbxIp
CSeq: 10 NOTIFY
Call-ID: 1234@$pbxIp
Event: check-sync;reboot=false
EOF
text "\nFile created successfully\n"
sleep 3
}
# the execute function permit to select cell to provision
execute() {
if compgen -G "${DIRECTORY}*.m900" >/dev/null; then
while true; do
text "Please select the M900 name"
set -- ${DIRECTORY}*.m900
i=0
for pathname; do
i=$((i + 1))
printf '%d) %s\n' "$i" "$pathname" >&2
done
echo -n 'Select m900 cell, or 0 to exit: ' >&2
read -r reply
number=$(printf '%s\n' "$reply" | tr -dc '[:digit:]')
if ! [[ $number =~ ^[0-9]+$ ]]; then
error "Not a number"
elif [ "$number" = "0" ]; then
text 'Bye!'
exit 0
elif [ "$number" -gt "$#" ]; then
text 'Invalid choice, try again'
else
break
fi
done
shift "$((number - 1))"
provisioning $1
else
error "No cell configured.\nPlease create a new cell first!${reset}"
sleep 3
fi
}
# the provisioning function send command to the m900
provisioning() {
local file="$1"
idCell="$(cat $file | awk -F"[:@]" '/To:/{print $3}')"
ipCell="$(cat $file | awk -F"[:@]" '/To:/{print $4}')"
ipPbx="$(cat $file | awk -F"[:@]" '/From:/{print $4}')"
sipsak -vvv -G -s sip:$idCell@$ipCell -H $ipPbx -f "$file"
sleep 3
}
menu() {
local error_msg=""
while :; do
clear
if [ "" != "$error_msg" ]; then
error "${error_msg}"
fi
echo -e "${green}***********************************${reset}"
echo -e "${blue}Select the task:"
echo -e " 1) Create a new cell Snom M900"
echo -e " 2) Reprovision an existing M900"
echo -e " 0) Exit"
echo -e "${green}***********************************${reset}"
read n
case $n in
1) create ;;
2) execute ;;
0) exit 1 ;;
*) error_msg="Invalid option" ;;
esac
done
}
# Start script
if ! [ -x "$(command -v sipsak)" ]; then
fail "\nError: sipsak is not installed!\n"
fi
# collect command line options
# default values for getopts
DIRECTORY="."
while getopts ":d:h" options; do
case "${options}" in #
d)
DIRECTORY=${OPTARG}
if ! [ -d "${DIRECTORY}" ]; then
fail "Directory '${DIRECTORY}' does not exist"
fi
;;
:)
fail "-${OPTARG} requires an argument."
;;
h | *)
usage
;;
esac
done
# add a / to the and of the directory if not present
DIRECTORY="$(echo ${DIRECTORY} | sed 's![^/]$!&/!')"
# start menu
menu