-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslhab.sh
More file actions
114 lines (105 loc) · 2.88 KB
/
slhab.sh
File metadata and controls
114 lines (105 loc) · 2.88 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
#!/bin/bash
##
## /usr/local/script/slhab.sh
## OpenHAB CLI script
##
## Created on 14 FEB 2015
## Version 1.1 dated 15 FEB 2015
##
## Arguments:
## -i <item> : item to be used
## -c <command> : command to use; if no command state is obtained
## -u <user> : alternate user
## -t : test mode
## -h : displays usage
# Set variables and default values
set -u
OHLOCALURLFILE="/srv/etc/openhab/url.cfg"
OHAUTHFILE="/srv/etc/openhab/users.cfg"
OHUSER=$(whoami)
OHAUTHSTR=""
OHITEM=""
OHCOMMAND=""
F_TEST=""
INITOPTSTR="-ksS"
HEADERSTR="Content-Type: text/plain"
EXEC=""
POSTEXECSTR=""
# Evaluate given options using getops; set variables accordingly
while getopts "i:c:u:ht" opt; do
case "$opt" in
\? | h)
printf -- "Usage: %s -i <item> [-c <command>] [-u <user>] [-t] [-h]\n" ${0##*/}
exit 2
;;
t)
F_TEST=TRUE
#printf -- "Test mode: script will only print the curl command for the REST-interface.\n"
;;
c)
OHCOMMAND=${OPTARG}
#printf -- "Command specified with -c: %s\n" ${OHCOMMAND}
;;
u)
OHUSER=${OPTARG}
#printf -- "Alternate user specified with -u: %s\n" ${OHUSER}
;;
i)
OHITEM=${OPTARG}
#printf -- "Item specified with -i: %s\n" ${OHITEM}
;;
:)
printf -- "Option -%s requires an argument. Exiting." ${OPTARG}
exit 1
;;
esac
done
# Check item
if [[ -z ${OHITEM} ]]
then
printf "No item specified. Exiting.\n"
exit 1
fi
# Get URL
OHLOCALURL=$(cat ${OHLOCALURLFILE})
# Check authentication
if [[ -z "${OHAUTHSTR}" ]]
then
OHAUTHSTR=$(cat ${OHAUTHFILE} | grep ${OHUSER} | awk 'BEGIN { FS = "=" } ; { print $2 }')
if [[ -z "${OHAUTHSTR}" ]]
then
USERSTR="-u ${OHUSER}"
else
# Prevent the password to be displayed in test mode
if [[ ${F_TEST} == TRUE ]]
then
USERSTR="-u ${OHUSER}:******"
else
OHAUTHSTR=${OHAUTHSTR%,*}
USERSTR="-u ${OHUSER}:${OHAUTHSTR}"
fi
OHAUTHSTR="" # Contained password and not needed anymore
fi
fi
# Compile execution command
if [[ -z ${OHCOMMAND} ]]
then
EXEC="curl ${INITOPTSTR} ${USERSTR} ${OHLOCALURL}/rest/items/${OHITEM}/state"
POSTEXECSTR="\\n"
else
EXEC="curl -H \"${HEADERSTR}\" ${INITOPTSTR} ${USERSTR} -X POST -d \"${OHCOMMAND}\" ${OHLOCALURL}/rest/items/${OHITEM}"
POSTEXECSTR=""
fi
unset USERSTR # Contained password and not needed anymore
# Execute
if [[ ${F_TEST} == TRUE ]]
then
printf "Test mode. Command that would have been executed in normal mode:\n %s\n" "${EXEC}"
EXITCODE=0
else
eval ${EXEC}
EXITCODE=$?
printf "${POSTEXECSTR}"
fi
unset EXEC # Contained password and not needed anymore
exit ${EXITCODE}