forked from DJM0/unifi-voucher-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunifi-api.sh
executable file
·181 lines (150 loc) · 4.46 KB
/
unifi-api.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
#!/bin/sh
#username=ubnt
#password=ubnt
#baseurl=https://unifi:8443
#site=default
[ -f ./unifi_sh_env ] && . ./unifi_sh_env
cookie=/tmp/unifi_cookie
curl_cmd="curl --tlsv1 --silent --cookie ${cookie} --cookie-jar ${cookie} --insecure "
named_args_to_payload() {
payload=""
for a in "$@" ; do
if [ "${a##*=*}" = "" ] ; then
k=`echo $a | cut -d = -f 1`
v=`echo $a | cut -d = -f 2`
payload="${payload}, '$k':'$v'"
fi
done
echo ${payload}
}
unifi_requires() {
if [ -z "$username" -o -z "$password" -o -z "$baseurl" -o -z "$site" ] ; then
echo "Error! please define required env vars before including unifi_sh. E.g. "
echo ""
echo "export username=ubnt"
echo "export password=ubnt"
echo "export baseurl=https://localhost:8443"
echo "export site=default"
echo ""
return
fi
}
unifi_login() {
# authenticate against unifi controller
${curl_cmd} --data "{'username':'$username', 'password':'$password'}" $baseurl/api/login
}
unifi_logout() {
# logout
${curl_cmd} $baseurl/logout
}
unifi_api() {
if [ $# -lt 1 ] ; then
echo "Usage: $0 <uri> [json]"
echo " uri example /stat/sta "
return
fi
uri=$1
shift
[ "${uri:0:1}" != "/" ] && uri="/$uri"
json="$@"
[ "$json" = "" ] && json="{}"
${curl_cmd} --data "$json" $baseurl/api/s/$site$uri
}
# cmd/stamgr
# authorize-guest(mac, minutes, [up=kbps, down=kbps, bytes=MB])
unifi_authorize_guest() {
if [ $# -lt 2 ] ; then
echo "Usage: $0 <mac> <minutes> [up=kbps] [down=kbps] [bytes=MB] [ap_mac=mac]"
return
fi
mac=$1
minutes=$2
other_payload=`named_args_to_payload "$@"`
${curl_cmd} --data "json={'cmd':'authorize-guest', 'mac':'${mac}', 'minutes':${minutes}${other_payload}}" $baseurl/api/s/$site/cmd/stamgr
}
# cmd/stamgr
# unauthorize-guest(mac)
unifi_unauthorize_guest() {
if [ $# -lt 1 ] ; then
echo "Usage: $0 <mac>"
return
fi
mac=$1
${curl_cmd} --data "json={'cmd':'unauthorize-guest', 'mac':'${mac}'}" $baseurl/api/s/$site/cmd/stamgr
}
# cmd/stamgr
# kick-sta(mac)
unifi_reconnect_sta() {
if [ $# -lt 1 ] ; then
echo "Usage: $0 <mac>"
return
fi
mac=$1
${curl_cmd} --data "json={'cmd':'kick-sta', 'mac':'${mac}'}" $baseurl/api/s/$site/cmd/stamgr
}
# cmd/stamgr
# block-sta(mac)
unifi_block_sta() {
if [ $# -lt 1 ] ; then
echo "Usage: $0 <mac>"
return
fi
mac=$1
${curl_cmd} --data "json={'cmd':'block-sta', 'mac':'${mac}'}" $baseurl/api/s/$site/cmd/stamgr
}
unifi_backup() {
if [ "$1" = "" ]; then
output=unifi-backup.unf # or `date +%Y%m%d`.unf
else
output=$1
fi
# ask controller to do a backup, response contains the path to the backup file
path=`$curl_cmd --data "json={'cmd':'backup'}" $baseurl/api/s/$site/cmd/system | sed -n 's/.*\(\/dl.*unf\).*/\1/p'`
# download the backup to the destinated output file
$curl_cmd $baseurl$path -o $output
}
# cmd/hotspot
# create-voucher(expires, n, [note=notes, up=kbps, down=kbps, bytes=MB])
# @returns create_time
unifi_create_voucher() {
if [ $# -lt 2 ] ; then
echo "Usage: $0 <minutes> <n> [note=notes] [up=kbps] [down=kbps] [bytes=MB]"
return
fi
minutes=$1
n=$2
other_payload=`named_args_to_payload "$@"`
token=`${curl_cmd} --data "json={'cmd':'create-voucher','expire':${minutes},'n':$n ${other_payload}}" $baseurl/api/s/$site/cmd/hotspot \
| sed -e 's/.*"create_time"\s*:\s*\([0-9]\+\).*/\1/'`
echo "token=$token"
if [ "$token" != "" ] ; then
${curl_cmd} --data "json={'create_time':${token}}" $baseurl/api/s/$site/stat/voucher
fi
}
# stat/voucher
# query(create_time)
unifi_get_vouchers() {
set -x
if [ $# -lt 0 ] ; then
echo "Usage: $0 [token]"
return
fi
token=$1
[ "$token" != "" ] && other_payload="'create_time':${token}"
${curl_cmd} --data "json={${other_payload}}" $baseurl/api/s/$site/stat/voucher
echo ${curl_cmd} --data "json={${other_payload}}" $baseurl/api/s/$site/stat/voucher
}
# delete-voucher(id)
unifi_delete_voucher() {
if [ $# -lt 1 ] ; then
echo "Usage: $0 <id>"
return
fi
id=$1
${curl_cmd} --data "json={'cmd':'delete-voucher','_id':'${id}'}" $baseurl/api/s/$site/cmd/hotspot
}
# stat/sta
unifi_list_sta() {
${curl_cmd} --data "json={}" $baseurl/api/s/$site/stat/sta
}
unifi_requires