Skip to content

Instantly share code, notes, and snippets.

@mbentley
Last active July 8, 2025 17:55
Show Gist options
  • Save mbentley/03c198077c81d52cb029b825e9a6dc18 to your computer and use it in GitHub Desktop.
Save mbentley/03c198077c81d52cb029b825e9a6dc18 to your computer and use it in GitHub Desktop.
Example API Calls Using Powershell and Bash/curl for Omada Controller (last validated on 5.12.7)
### PowerShell Example
# set variables
$OMADA_URL = "https://omada.example.com:8043"
$USERNAME = "admin"
$PASSWORD = "test12345"
# get controller id from the API
$CONTROLLER_ID = (Invoke-RestMethod -Uri "${OMADA_URL}/api/info" -Method Get -UseBasicParsing).result.omadacId
# set the login request body as json
$loginRequestBody = @{
username = $USERNAME
password = $PASSWORD
} | ConvertTo-Json
# login, get token, set a session variable
$loginResponse = Invoke-RestMethod -Uri "${OMADA_URL}/${CONTROLLER_ID}/api/v2/login" -Method Post -ContentType "application/json" -Body $loginRequestBody -SessionVariable OmadaSession
# extract the token and create a variable for the headers
$TOKEN = $loginResponse.result.token
$RequestHeaders = @{
"Csrf-Token" = $TOKEN
"Content-Type" = "application/json"
}
# validate login
Invoke-RestMethod -Uri "${OMADA_URL}/${CONTROLLER_ID}/api/v2/loginStatus?token=${TOKEN}" -Method Get -Headers $RequestHeaders -WebSession $OmadaSession
# example to get info on the current user
Invoke-RestMethod -Uri "${OMADA_URL}/${CONTROLLER_ID}/api/v2/users/current?token=${TOKEN}&currentPage=1&currentPageSize=1000" -Method Get -Headers $RequestHeaders -WebSession $OmadaSession
### Bash Example
# set variables
OMADA_URL="https://omada.example.com:8043"
USERNAME="admin"
PASSWORD="test12345"
# get controller id from the API
CONTROLLER_ID="$(curl -sk "${OMADA_URL}/api/info" | jq -r .result.omadacId)"
# login, get token, set & use cookies
TOKEN="$(curl -sk -X POST -c "/tmp/omada-cookies.txt" -b "/tmp/omada-cookies.txt" -H "Content-Type: application/json" "${OMADA_URL}/${CONTROLLER_ID}/api/v2/login" -d '{"username": "'"${USERNAME}"'", "password": "'"${PASSWORD}"'"}' | jq -r .result.token)"
# once logged in, make sure you add the following header on additional API calls:
# -H "Csrf-Token: ${TOKEN}"
# validate login
curl -sk -X GET -b "/tmp/omada-cookies.txt" -H "Content-Type: application/json" -H "Csrf-Token: ${TOKEN}" "${OMADA_URL}/${CONTROLLER_ID}/api/v2/loginStatus?token=${TOKEN}" | jq .
# example to get info on the current user
curl -sk -X GET -b "/tmp/omada-cookies.txt" -H "Content-Type: application/json" -H "Csrf-Token: ${TOKEN}" "${OMADA_URL}/${CONTROLLER_ID}/api/v2/users/current?token=${TOKEN}&currentPage=1&currentPageSize=1000"
@smcgann99
Copy link

smcgann99 commented Dec 3, 2024

Thanks for replying, I managed to sort this now.

@achetronic
Copy link

Thank you for the example @mbentley. It works smoothly

@Ziwa001
Copy link

Ziwa001 commented Jul 8, 2025

#!/bin/bash
#> Example API Calls Using Powershell and Bash/curl for Omada Controller (last validated on 5.12.7) - mbentley
#> REF :::: https://gist.github.com/mbentley/03c198077c81d52cb029b825e9a6dc18
#> Comments and Edits - Ibrahim Kiptoo Kibowen ([email protected])
#> Tunawakilisha +254 (Representing the +254)

#> ## Bash Example
#> set variables

OMADA_URL="https://192.168.7.38:8043"
USERNAME="tuxhot"
PASSWORD="Ugali@a95H"

#>#**************************************************************************************************
#>#MOD :::: BEGIN :::: COMMENT & DISCUSSION - July 8th, 2025 - filtering incoming json array in bash
#>#**************************************************************************************************
#> Niletee mandazi tatu na chapo mbili changanya na maharagwe ya finje!
#> (Bring three mandazis and two chapatis mixed with beans for fifty bob!)

#bash-5.1$ jq
#>bash: jq: command not found
#> OK - filtering without jq tool (JSON Processing tool :)
#> OS info
#> Distro: Slackware 15.0
#> bash: GNU bash, version 5.1.16(1)

#>LETS ROLL!
#>debug - testing
#>dump echo below in file called omadaapi.sh and remember to make file executable
#in bash skript, escape each double quote with a backslash

echo "{\"errorCode\":0,\"msg\":\"Success.\",\"result\":{\"controllerVer\":\"5.14.26.1\",\"apiVer\":\"3\",\"configured\":true,\"type\":1,\"supportApp\":true,\"omadacId\":\"e72e7b8b12b007a8595af9a64b76e57c\",\"registeredRoot\":true,\"omadacCategory\":\"advanced\",\"mspMode\":false}}"

#./omadaapi.sh|grep -oE '\"omadacId(\"\:\"[[:alnum:]]*\")'
#> "omadacId":"e72e7b8b12b007a8595af9a64b76e57c"
#>[[:alnum:]] --> alphanumeric match
#. -E extended & -o only exact string in sentence
#>"omadacId(":"[[:alnum:]]*") -->omadacId()
#> added flags -i for case insenstive
#./omadaapi.sh|grep -ioE '\"omadacId(\"\:\"[[:alnum:]]*\")'
#> "omadaCId":"e72e7b8b12b007a8595af9a64b76e57c"
#> remove evertying before delimter --> ":"
#> (REF :::: https://askubuntu.com/questions/438383/how-can-i-use-sed-to-remove-all-characters-before-a-specific-symbol)
#./omadaapi.sh|grep -ioE '\"omadacId(\"\:\"[[:alnum:]]*\")'|sed 's/^[^:]*:/:/'
#> :"e72e7b8b12b007a8595af9a64b76e57c"
#> same as above but also removes delimtere
#./omadaapi.sh|grep -ioE '\"omadacId(\"\:\"[[:alnum:]]*\")'|sed 's/^[^:]*://'
#> "e72e7b8b12b007a8595af9a64b76e57c"
#>remove double quotes
# ./omadaapi.sh|grep -ioE '\"omadacId(\"\:\"[[:alnum:]]*\")'|sed 's/^[^:]*://'|sed 's/\"//g'
#> e72e7b8b12b007a8595af9a64b76e57c
#> same as above but removing any spaces :)
# ./omadaapi.sh|grep -ioE '\"omadacId(\"\:\"[[:alnum:]]*\")'|sed 's/^[^:]*://'|sed 's/\"//g'|tr -d ' '
#> e72e7b8b12b007a8595af9a64b76e57c
##**************************************************************************************************
##MOD :::: END :::: COMMENT & DISCUSSION - July 8th, 2025 - filtering incoming json array in bash
##**************************************************************************************************

#> get controller id from the API
#> original curl# CONTROLLER_ID="$(curl -sk "${OMADA_URL}/api/info" | jq -r .result.omadacId)"
#> debug
#curl -sk "${OMADA_URL}/api/info"
#> {"errorCode":0,"msg":"Success.","result":{"controllerVer":"5.14.26.1","apiVer":"3","configured":true,"type":1,"supportApp":true,"omadacId":"e72e7b8b12b007a8595af9a64b76e57c","registeredRoot":true,"omadacCategory":"advanced","mspMode":false}}:

#>MOD :::: July 8th, 2025 - filtering incoming json string to get omadaid (see discussion above)
CONTROLLER_ID="$(curl -sk "${OMADA_URL}/api/info"|grep -ioE '\"omadacId(\"\:\"[[:alnum:]]*\")'|sed 's/^[^:]*://'|sed 's/\"//g'|tr -d ' ')"

#> login, get token, set & use cookies
#>original token call
#TOKEN="$(curl -sk -X POST -c "/tmp/omada-cookies.txt" -b "/tmp/omada-cookies.txt" -H "Content-Type: application/json" "${OMADA_URL}/${CONTROLLER_ID}/api/v2/login" -d '{"username": "'"${USERNAME}"'", "password": "'"${PASSWORD}"'"}' | jq -r .result.token)"

#> debug
# curl -sk -X POST -c "/tmp/omada-cookies.txt" -b "/tmp/omada-cookies.txt" -H "Content-Type: application/json" "${OMADA_URL}/${CONTROLLER_ID}/api/v2/login" -d '{"username": "'"${USERNAME}"'", "password": "'"${PASSWORD}"'"}'
# {"errorCode":0,"msg":"Log in successfully.","result":{"omadacId":"e72e7b8b12b007a8595af9a64b76e57c","token":"1ec0b34707ae449b8846fba1ddca94de"}}

#>MOD ::: July 8th, 2025 - - filtering incoming json string to get token
TOKEN="$(curl -sk -X POST -c "/tmp/omada-cookies.txt" -b "/tmp/omada-cookies.txt" -H "Content-Type: application/json" "${OMADA_URL}/${CONTROLLER_ID}/api/v2/login" -d '{"username": "'"${USERNAME}"'", "password": "'"${PASSWORD}"'"}'|grep -ioE '\"token(\"\:\"[[:alnum:]]*\")'|sed 's/^[^:]*://'|sed 's/\"//g'|tr -d ' ')"

#> once logged in, make sure you add the following header on additional API calls:
#> -H "Csrf-Token: ${TOKEN}"

#> validate login
#> original call# curl -sk -X GET -b "/tmp/omada-cookies.txt" -H "Content-Type: application/json" -H "Csrf-Token: ${TOKEN}" "${OMADA_URL}/${CONTROLLER_ID}/api/v2/loginStatus?token=${TOKEN}" | jq .
#>MOD :::: July 8th, 2025 - removed pipe to jq
curl -sk -X GET -b "/tmp/omada-cookies.txt" -H "Content-Type: application/json" -H "Csrf-Token: ${TOKEN}" "${OMADA_URL}/${CONTROLLER_ID}/api/v2/loginStatus?token=${TOKEN}"
#> {"errorCode":0,"msg":"Success.","result":{"login":true}}

#> example to get info on the current user
curl -sk -X GET -b "/tmp/omada-cookies.txt" -H "Content-Type: application/json" -H "Csrf-Token: ${TOKEN}" "${OMADA_URL}/${CONTROLLER_ID}/api/v2/users/current?token=${TOKEN}&currentPage=1&currentPageSize=1000"
#> {"errorCode":0,"msg":"Success.","result":{"id":"686d133f58d5f00e041539fe","type":0,"roleId":"admin_id","roleName":"Administrator","name":"tuxhot","email":"[email protected]","omadacId":"e72e7b8b12b007a8595af9a64b76e57c","privilege":{"sites":[{"name":"hut","category":"advanced","key":"68447efde21e9b2f114123ae"}],"lastVisited":"68447efde21e9b2f114123ae","lastSiteCategory":"advanced","all":true},"disaster":0,"needFeedback":true,"forceModify":false,"dbnormal":true,"view":0,"root":false,"userLevel":0}}

#> Sasa hii chapo ndogo hivi, nani atashiba
#> (Who will be satisified with such small chapatis)
#> Tunawakilisha +254 (Representing the +254)

:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment