Logic Machine Forum
shell script to LUA - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: shell script to LUA (/showthread.php?tid=2450)



shell script to LUA - edos - 03.02.2020

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



RE: shell script to LUA - admin - 04.02.2020

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.


RE: shell script to LUA - edos - 04.02.2020

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!


RE: shell script to LUA - admin - 05.02.2020

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



RE: shell script to LUA - edos - 05.02.2020

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


RE: shell script to LUA - admin - 05.02.2020

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),
  })



RE: shell script to LUA - edos - 05.02.2020

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.


RE: shell script to LUA - admin - 05.02.2020

Let me know if you have any problems with parsing JSON data.


RE: shell script to LUA - benanderson_475 - 23.03.2020

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)



RE: shell script to LUA - admin - 23.03.2020

See this: https://forum.logicmachine.net/showthread.php?tid=427&pid=15575#pid15575