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.

Reset POE Port on Unifi Switch - Connecting to a device via SSH Key
#6
(03.02.2021, 02:43)jamesng Wrote: Hi 

I'm attempting to control Unifi US-48-500W and US-8-60W switches (all running the latest firmware - cloud key firmware 2.0.27 which changes the access url from https://controllerip:8443 to https://controllerip/network/site) and I think breaks the reverse engineered API.

I was under the impression that the API (https://ubntwiki.com/products/software/u...roller/api) was a reverse engineered project and that the telnet control which was officially supported by Unifi.

Is there an example of how to make the SSH connection and execute the telnet commands with Logic Machine? 

Kind Regards
James

Hi jamesng, 
from the last unifi update 2 things have changed, 1- controller access is no longer avalible on port 8443, this is now https on port 443 and 2- end points have changed for login endpoint  api/auth/login and for api endpoint prefix with /proxy/network see below code , change to suit your requirements 

Code:
htps = require("ssl.https")
       require('json')

htps.TIMEOUT = 5

usr_credenitals = json.encode({
    username = 'XXXXXXXXXXXXXX',
  password = 'XXXXXXXX'
})

login = 'https://192.168.1.10:443/api/auth/login'

sta = 'https://192.168.1.10:443/proxy/network/api/s/d5gh60j5/stat/sta'
                             
get_sites = 'https://192.168.1.10:443/proxy/network/api/self/sites'

Post_response = {}

local res, err, response_headers, status = htps.request
  {
    url = login,
    method = "POST",
    protocol = 'tlsv12',
    headers =
    {
    ["Accept"] = "*/*",
    ["Content-Type"] = "application/json",
    ["Content-Length"] = usr_credenitals:len()
    },

  source = ltn12.source.string(usr_credenitals),     
  sink = ltn12.sink.table(Post_response)
  }

if Post_response then
  --log(res, code, response_headers)
mmm_cookies = response_headers['set-cookie']
  if mmm_cookies then
  token = mmm_cookies:match('(TOKEN=[^;]+)'..';')
  end

  Get_response ={}

  res, err = htps.request({
     url = sta,
     method = "GET",
     protocol = 'tlsv12',
     headers = {
     cookie = token,
    ['X-CSRF-Token'] = response_headers['x-csrf-token']
    },
    sink = ltn12.sink.table(Get_response),
  })

  if Get_response then

    Get_response = table.concat(Get_response)
    dec = json.decode(Get_response)
   
    -- get all mac adresses currently connected to site
    for i = 1, #dec.data do
      log(dec.data[i].mac)
      -- return boolean if mac adress is connected
      is_mac = dec.data[i].mac =='42:45:05:39:0f:d9'
     
     if is_mac then
        log(is_mac)
      end
    end
   
  else
    log('could not get stat ' .. tostring(err))
  end
  else
  log('login failed ' .. tostring(err))
end
Reply


Messages In This Thread
RE: Reset POE Port on Unifi Switch - Connecting to a device via SSH Key - by benanderson_475 - 03.02.2021, 03:43

Forum Jump: