Posts: 15
Threads: 1
Joined: Jan 2016
Reputation:
2
Hello,
Can anyone tell me if this sample script can be "transplanted" to LM?
Code: #!/bin/bash
username=user
password=password
baseurl=https://IP:8443
cookie=/tmp/unifi_cookie
curl_cmd="curl --silent --cookie ${cookie} --cookie-jar ${cookie} --insecure "
#login
${curl_cmd} --data "{'username':'$username', 'password':'$password'}" $baseurl/api/login > /dev/null 2>&1
${curl_cmd} $baseurl/api/s/default/stat/sta > /var/www/scripts/unifi-users.json
#logout
${curl_cmd} $baseurl/logout > /dev/null 2>&1
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
See this thread for a similar example: https://forum.logicmachine.net/showthread.php?tid=1921
First request is POST so you need to pass data like this:
Code: data = "{'username':'user', 'password':'1234'}"
res, err, hdrs = socket.http.request(url1, data)
If running an older FW you might also need to change socket.http to ssl.https in order to use https. Newer FW will detect https automatically.
Posts: 15
Threads: 1
Joined: Jan 2016
Reputation:
2
Thanks I looked an example! Here is the result:
Code: require('socket.http')
socket.http.TIMEOUT = 5
data = "{'username':'user', 'password':'12345'}"
url1 = 'https://10.10.11.2:8443/api/login'
url2 = 'https://10.10.11.2:8443/api/s/default/stat/sta'
res, err, hdrs = socket.http.request(url1,data)
if res then
tbl = {}
res, err = socket.http.request({
url = url2,
headers = {
cookie = hdrs['set-cookie']
},
sink = ltn12.sink.table(tbl),
})
if res then
resp = table.concat(tbl)
log(resp)
else
alert('request 2 failed: ' .. tostring(err))
end
else
alert('request 1 failed: ' .. tostring(err))
end
But it still gives an error.
Code: * string: {"meta":{"rc":"error","msg":"api.err.LoginRequired"},"data":[]}
I am not good at scripts, may you help me ?
Thanks a lot!
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
Log what you get during login request. I've also changed data variable to contain valid JSON:
Code: require('json')
http = require('socket.http')
http.TIMEOUT = 5
data = json.encode({
username = 'user',
password = '12345'
})
url1 = 'https://10.10.11.2:8443/api/login'
url2 = 'https://10.10.11.2:8443/api/s/default/stat/sta'
res, err, hdrs = http.request(url1, data)
log(res, err, hdrs)
if res then
tbl = {}
res, err = http.request({
url = url2,
headers = {
cookie = hdrs['set-cookie']
},
sink = ltn12.sink.table(tbl),
})
if res then
resp = table.concat(tbl)
log(resp)
else
alert('request 2 failed: ' .. tostring(err))
end
else
alert('request 1 failed: ' .. tostring(err))
end
Posts: 15
Threads: 1
Joined: Jan 2016
Reputation:
2
05.02.2020, 08:53
(This post was last modified: 05.02.2020, 09:00 by edos.)
Yes, now authorization is ok. After changing the data variable to store valid JSON
Here is the log:
Code: * arg: 1
* string: {"meta":{"rc":"ok"},"data":[]}
* arg: 2
* number: 200
* arg: 3
* table:
["access-control-expose-headers"]
* string: Access-Control-Allow-Origin,Access-Control-Allow-Credentials
["content-type"]
* string: application/json;charset=UTF-8
["connection"]
* string: close
["content-length"]
* string: 30
["access-control-allow-credentials"]
* string: true
["date"]
* string: Wed, 05 Feb 2020 08:42:18 GMT
["x-frame-options"]
* string: DENY
["set-cookie"]
* string: unifises=ghgjhghtYTfghjgGHjhFh; Path=/; Secure; HttpOnly, csrf_token=hgYTGHjhg656GHGhghvfr6^ftr; Path=/; Secure
["vary"]
* string: Origin
But I do not see the answer on url2.
Thanks
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
Some extra parsing of cookie header is required. Try this for the second request:
Code: cookies = hdrs['set-cookie']
session = cookies:match('(unifises=[^;]+)')
token = cookies:match('(csrf_token=[^;]+)')
cookie = session .. '; ' .. token
res, err = http.request({
url = url2,
headers = {
cookie = cookie,
},
sink = ltn12.sink.table(tbl),
})
Posts: 15
Threads: 1
Joined: Jan 2016
Reputation:
2
Yes, I get everything perfectly.
Thank you very much.
I will try to parse the resulting JSON.
P.S. If I can!
For information on the forum, I report that it was an API UNIFI Controller. Getting information about clients: connected or not, online time, and a lot of similar information. I did not find such an implementation on this forum.
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
Let me know if you have any problems with parsing JSON data.
Posts: 139
Threads: 44
Joined: Dec 2017
Reputation:
4
I have the following error, what could it be?, i have also tried ssl.https instead of socket.http, it returns the same error...?
arg: 1
* nil
* arg: 2
* string: error:1408F10B:lib(20):func(143):reason(267)
* arg: 3
* nil
Code: http = require("socket.http")
ltn12 = require("ltn12")
require('json')
http.TIMEOUT = 5
data = json.encode({
username = 'user',
password = 'pasword1234'
})
url1 = 'https://192.168.1.10:8443/api/login'
--url2 = 'https://192.168.1.10:8443/api/s/default/stat/sta'
res, err, hdrs = http.request(url1, data)
log(res, err, hdrs)
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
|