This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

shell script to LUA
#1
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
Reply
#2
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.
Reply
#3
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!
Reply
#4
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
Reply
#5
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
Reply
#6
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),
  })
Reply
#7
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.
Reply
#8
Let me know if you have any problems with parsing JSON data.
Reply
#9
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)
Reply
#10
See this: https://forum.logicmachine.net/showthrea...5#pid15575
Reply


Forum Jump: