-
Notifications
You must be signed in to change notification settings - Fork 14
/
wemo
executable file
·446 lines (425 loc) · 18.5 KB
/
wemo
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#!/bin/bash
#123456789o123456789o123456789o123456789o123456789o123456789o123456789o123456789
# Belkin WeMo Command Line Tool #
#123456789o123456789o123456789o123456789o123456789o123456789o123456789o123456789
#123456789o123456789o123456789o123456789o123456789o123456789o123456789o123456789
# Exit Codes #
#==============================================================================#
# 10 Main: Argument Error #
# 11 Main: Run Action Error #
# 20 restPostXmlStdinBelkinSoap: Argument Error #
# 21 restPostXmlStdinBelkinSoap: Missing/Empty URL #
# 22 restPostXmlStdinBelkinSoap: Missing/Emtpy SOAP Header #
#123456789o123456789o123456789o123456789o123456789o123456789o123456789o123456789
COPYRIGHT="2015 James Borkowski @github:agilemation" # © Notice, displayed with --help
ACKNOWLEDGEMENT="Third-party trademarks mentioned are the property of their respective owners."
USAGE_TEXT="-h [ IP/HOSTNAME ] -a [ ACTION ] [[ -ver ] | [ -? ]] | [ -s ] | [ -v ] | [ -V ] | [ -t ] | [ -T ]] [[ -tt ]]
Arguments:
[ -h | --host ] IP_ADDRESS | HOSTNAME
[ -a | --action ] ON | OFF | GETSTATE | GETSIGNALSTRENGTH | GETNAME | SETNAME NAME
Optional Arguments:
[ -s | --silent ]
[ -v | --verbose ]
[ -V | --very-verbose ] }
[ -t | --trace ] } [ -tt | --trace-time ]
[ -T | --trace-raw ] }
[ -ver | --version ]
[ -? | --help ]"
# Core Functions and Alias definitions Used For Script Setup
getScriptFile () { basename "${BASH_SOURCE[0]}" ; } # This filename as executed
getScriptPath () { local f="${BASH_SOURCE[0]}" ; local p ; while test -L "${f}"
do p="$( cd -P "$( dirname "${f}" )" && pwd )" ; f="$( readlink "${f}" )" &&
f="${p}/${f}" ; done ; p="$( cd -P "$( dirname "${f}" )" && pwd )" ;
printf "%s\n" "${p}" ; }
getJsonValueSimple () { grep -i "${1}" "${2}" | head -1 | awk -F: \
'{ print $2 }' | sed -e 's/[",]//g' -e 's/^\ //g' ; }
alias handleOutput='cat -' # Overide this if you want to affect the output
# Core Variables
TITLE="Belkin WeMo Command Line Tool"
SCRIPTpath="$( getScriptPath )"
SCRIPTfile="$( getScriptFile )"
VERSION="$( getJsonValueSimple "version" "${SCRIPTpath}/package.json" )"
DESCRIPTION="$( getJsonValueSimple "description" "${SCRIPTpath}/package.json" )"
# Source any external files needed
. "${SCRIPTpath}/functions.inc.sh"
#123456789o123456789o123456789o123456789o123456789o123456789o123456789o123456789
# Local Functions #
#123456789o123456789o123456789o123456789o123456789o123456789o123456789o123456789
usage ()
{
# Leaving this here for a bit as it serves as a reminder to myself that I am
# sane to have implemented my toot, tell, talk, yelp and yell comands as a
# fixed width echo command set - allowing avoidence of all this.....
printf "\n%s\n" "${TITLE} v${VERSION}"
printf "\n%s\n" "${DESCRIPTION}" | fold -sw 80
printf "\n%s\n" "Copyright © ${COPYRIGHT}"
printf "%s\n" "${ACKNOWLEDGEMENT}"
printf "%s\n\n" "Usage:
$ ${SCRIPTfile} ${USAGE_TEXT}"
}
usage ()
{
# Verify if we need this.....
# Report usage to the user as follows
yell "${TITLE} v${VERSION}"
tell "${DESCRIPTION}" | fold -sw 80
yelp
tell "Copyright © ${COPYRIGHT}"
tell "${ACKNOWLEDGEMENT}"
yell "Usage:
$ ${SCRIPTfile} ${USAGE_TEXT}"
}
restPostXmlStdinBelkinSoap ()
{
# This funciton takes an XML SOAP payload from Standard In and creates an
# HTTP POST that it submits to the Belkin WeMo, it offers variuous levels of
# debug, nameley - [[ -v | --verbose ] [ -t | --trace]
# [ -T | --trace-raw ]] opt: [[ -tt | --trace-time ]]
# N.B. Trace Time is an optional addition to append the times to each of the
# Previous forms of debugging.
# Parse Arguments
local URL=""
local SOAP_HEADER=""
local VERBOSE_FLAGstr=""
local TRACE_FLAGstr=""
local TRACE_TIME_FLAGstr=""
while testArgs
do
case "${1}" in
-u | --url )
shift
URL="${1}"
shift
;;
-s | --soap )
shift
SOAP_HEADER="${1}"
shift
;;
-v | --verbose )
shift
VERBOSE_FLAGstr="--verbose"
;;
-t | --trace )
shift
TRACE_FLAGstr="--trace-ascii -"
;;
-T | --trace-raw )
shift
TRACE_FLAGstr="--trace -"
;;
-tt | --trace-time )
shift
TRACE_TIME_FLAGstr="--trace-time"
;;
* )
echoError "${FUNCNAME}: The argument '${1}' of remaining arguments '$@' is unhandled, Exiting..."
shift $#
finish 20
;;
esac
done
# Final check before executing
testEmpty "${URL}" && echoError "${FUNCNAME}: You must provide a URL" && finish 21
testEmpty "${SOAP_HEADER}" && echoError "${FUNCNAME}: You must provide a SOAP_HEADER" && finish 22
# Start of curl execution
curl \
--silent \
--show-error \
${VERBOSE_FLAGstr} \
${TRACE_FLAGstr} \
${TRACE_TIME_FLAGstr} \
--http1.0 \
--user-agent '' \
--request POST \
--header 'Accept: ' \
--header 'Content-type: text/xml; charset="utf-8"' \
--header 'SOAPACTION: "'"${SOAP_HEADER}"'"' \
--data @- \
--url "${URL}"
}
testExecAvailiable ()
{
which "${1}" 1>/dev/null 2>/dev/null ;
}
filterXmlClean ()
{
# Clean XML as long as we are not filtering the value from it or using any of the
# trace modes of curl as that will muck the well formed XML output and make xmllint
# have issues
if testBoolFalse "${EXTRACT_VALUE_FROM_RESPONSE}" &&
testEmpty "${TRACE_FLAGstr}" &&
testEmpty "${TRACE_TIME_FLAGstr}"
then
# Make sure we have xmllint
if testExecAvailiable xmllint
then
# We do, lets use it
xmllint --format -
else
# We don't so lets just pass through
cat -
fi
else
# We are filtering the value so can't use xmllint as it's not well formed XML
cat -
fi
}
filterExtractValue ()
{
# If we are running in any of the debug modes, we don't want to alter output
if testBoolTrue "${EXTRACT_VALUE_FROM_RESPONSE}"
then
# Normal run, so lets grab just the value the user wants to see
grep '<BinaryState>\|<SignalStrength>\|<FriendlyName>' |
cut -d '>' -f2 |
cut -d '<' -f1
else
# Running in a debug/verbose/trace mode so lets just let it pass through
cat -
printf '\n'
fi
}
filterHumanizeBinary ()
{
# If we are running in any of the debug modes, we don't want to alter output
if testBoolTrue "${EXTRACT_VALUE_FROM_RESPONSE}"
then
# Normal run, so lets change '0's to OFF and '1's to ON
sed -e 's/0/OFF/g' -e 's/1/ON/g'
else
# Running in a debug/verbose/trace mode so lets just let it pass through
cat -
fi
}
#123456789o123456789o123456789o123456789o123456789o123456789o123456789o123456789
# Execution Start Point - Martial Arguments #
#123456789o123456789o123456789o123456789o123456789o123456789o123456789o123456789
EXTRACT_VALUE_FROM_RESPONSE="TRUE"
RUN_ACTION=""
SET_NAME=""
VERBOSE_FLAGstr=""
TRACE_FLAGstr=""
TRACE_TIME_FLAGstr=""
while testArgs
do
case ${1} in
-ver | --version )
shift
echo "v${VERSION}"
finish 0
;;
-h | --host )
shift
DEVICE_HOST="${1}"
shift
;;
-a | --action )
shift
case ${1} in
[Gg][Ee][Tt][Ss][Tt][Aa][Tt][Ee] | \
[Oo][Nn] | \
[Oo][Ff][Ff] | \
[Gg][Ee][Tt][Ss][Ii][Gg][Nn][Aa][Ll][Ss][Tt][Rr][Ee][Nn][Gg][Tt][Hh] | \
[Gg][Ee][Tt][Nn][Aa][Mm][Ee] )
RUN_ACTION="${1}"
shift
;;
[Ss][Ee][Tt][Nn][Aa][Mm][Ee] )
RUN_ACTION="${1}"
shift
SET_NAME="${1}"
shift
;;
* )
echoError "${SCRIPTfile}: The argument given as an action '${1}' of remaining arguments '$@' is unhandled, Exiting..."
usage
shift $#
finish 11
;;
esac
;;
-s | --silent )
shift
alias handleOutput='toDevNull' # Overriding the pass-through def. above
;;
-v | --verbose )
shift
EXTRACT_VALUE_FROM_RESPONSE="FALSE"
;;
-V | --very-verbose )
shift
VERBOSE_FLAGstr="--verbose"
EXTRACT_VALUE_FROM_RESPONSE="FALSE"
;;
-t | --trace )
shift
TRACE_FLAGstr="--trace"
EXTRACT_VALUE_FROM_RESPONSE="FALSE"
;;
-T | --trace-raw )
shift
TRACE_FLAGstr="--trace-raw"
EXTRACT_VALUE_FROM_RESPONSE="FALSE"
;;
-tt | --trace-time )
shift
TRACE_TIME_FLAGstr="--trace-time"
;;
-? | --help )
shift
usage
finish 0
;;
* )
echo "The argument '${1}' of remaining arguments '$@' is unhandled, Exiting..."
usage
shift $#
finish 10
;;
esac
done
#123456789o123456789o123456789o123456789o123456789o123456789o123456789o123456789
# Identify TCP port to use #
#123456789o123456789o123456789o123456789o123456789o123456789o123456789o123456789
PORTTEST="$( curl --silent ${DEVICE_HOST}:49153 | grep "404" )"
if testEmpty "${PORTTEST}"
then
PORT=49152
else
PORT=49153
fi
#123456789o123456789o123456789o123456789o123456789o123456789o123456789o123456789
# Final test that we have everything we need to run, then do it... #
#123456789o123456789o123456789o123456789o123456789o123456789o123456789o123456789
if testEmpty "${DEVICE_HOST}" || testEmpty "${RUN_ACTION}"
then
usage
else
case "${RUN_ACTION}" in
[Gg][Ee][Tt][Ss][Tt][Aa][Tt][Ee] )
printf '%s\n' \
'<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetBinaryState xmlns:u="urn:Belkin:service:basicevent:1" />
</s:Body>
</s:Envelope>' |
restPostXmlStdinBelkinSoap \
${VERBOSE_FLAGstr} ${TRACE_FLAGstr} ${TRACE_TIME_FLAGstr} \
--soap 'urn:Belkin:service:basicevent:1#GetBinaryState' \
--url "http://${DEVICE_HOST}:${PORT}/upnp/control/basicevent1" |
filterExtractValue |
filterHumanizeBinary |
handleOutput |
filterXmlClean
;;
[Oo][Nn] )
printf '%s\n' \
'<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:SetBinaryState xmlns:u="urn:Belkin:service:basicevent:1">
<BinaryState>1</BinaryState>
</u:SetBinaryState>
</s:Body>
</s:Envelope>' |
restPostXmlStdinBelkinSoap \
${VERBOSE_FLAGstr} ${TRACE_FLAGstr} ${TRACE_TIME_FLAGstr} \
--soap 'urn:Belkin:service:basicevent:1#SetBinaryState' \
--url "http://${DEVICE_HOST}:${PORT}/upnp/control/basicevent1" |
filterExtractValue |
filterHumanizeBinary |
handleOutput |
filterXmlClean
;;
[Oo][Ff][Ff] )
printf '%s\n' \
'<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:SetBinaryState xmlns:u="urn:Belkin:service:basicevent:1">
<BinaryState>0</BinaryState>
</u:SetBinaryState>
</s:Body>
</s:Envelope>' |
restPostXmlStdinBelkinSoap \
${VERBOSE_FLAGstr} ${TRACE_FLAGstr} ${TRACE_TIME_FLAGstr} \
--soap 'urn:Belkin:service:basicevent:1#SetBinaryState' \
--url "http://${DEVICE_HOST}:${PORT}/upnp/control/basicevent1" |
filterExtractValue |
filterHumanizeBinary |
handleOutput |
filterXmlClean
;;
[Gg][Ee][Tt][Ss][Ii][Gg][Nn][Aa][Ll][Ss][Tt][Rr][Ee][Nn][Gg][Tt][Hh] )
printf '%s\n' \
'<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetSignalStrength xmlns:u="urn:Belkin:service:basicevent:1">
<GetSignalStrength>0</GetSignalStrength>
</u:GetSignalStrength>
</s:Body>
</s:Envelope>' |
restPostXmlStdinBelkinSoap \
${VERBOSE_FLAGstr} ${TRACE_FLAGstr} ${TRACE_TIME_FLAGstr} \
--soap 'urn:Belkin:service:basicevent:1#GetSignalStrength' \
--url "http://${DEVICE_HOST}:${PORT}/upnp/control/basicevent1" |
filterExtractValue |
handleOutput |
filterXmlClean
;;
[Gg][Ee][Tt][Nn][Aa][Mm][Ee] )
printf '%s\n' \
'<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetFriendlyName xmlns:u="urn:Belkin:service:basicevent:1">
</u:GetFriendlyName>
</s:Body>
</s:Envelope>' |
restPostXmlStdinBelkinSoap \
${VERBOSE_FLAGstr} ${TRACE_FLAGstr} ${TRACE_TIME_FLAGstr} \
--soap 'urn:Belkin:service:basicevent:1#GetFriendlyName' \
--url "http://${DEVICE_HOST}:${PORT}/upnp/control/basicevent1" |
filterExtractValue |
handleOutput |
filterXmlClean
;;
[Ss][Ee][Tt][Nn][Aa][Mm][Ee] )
printf '%s\n' \
'<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:ChangeFriendlyName xmlns:u="urn:Belkin:service:basicevent:1">
<FriendlyName>'"${SET_NAME}"'</FriendlyName>
</u:ChangeFriendlyName>
</s:Body>
</s:Envelope>' |
restPostXmlStdinBelkinSoap \
${VERBOSE_FLAGstr} ${TRACE_FLAGstr} ${TRACE_TIME_FLAGstr} \
--soap 'urn:Belkin:service:basicevent:1#ChangeFriendlyName' \
--url "http://${DEVICE_HOST}:${PORT}/upnp/control/basicevent1" |
filterExtractValue |
handleOutput |
filterXmlClean
;;
"" )
echo
echoError "${SCRIPTfile}: Need to specify an ACTION as an argument"
usage
;;
* )
echo
echoError "${SCRIPTfile}: Action Not Recognized: '${RUN_ACTION}'"
usage
;;
esac
fi