Skip to content

Instantly share code, notes, and snippets.

@Naman1997
Created September 23, 2023 04:21
Show Gist options
  • Save Naman1997/e3d2366ae9f6afd165079507a11b8a81 to your computer and use it in GitHub Desktop.
Save Naman1997/e3d2366ae9f6afd165079507a11b8a81 to your computer and use it in GitHub Desktop.
Shell scripts for creating and deleting dhcp reservations using API calls with Omada Controller
#!/bin/bash
# Example execution: create_dhcp_reservation.sh C2-F3-D9-37-2A-33 https://omada.hosted:8043 username password description
MAC_ADDRESS=$1
OMADA_URL=$2
USERNAME=$3
PASSWORD=$4
DESCRIPTION=$5
# 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)"
# get the default site id
SITE_ID="$(curl -ks -b "/tmp/omada-cookies.txt" -H "Content-Type: application/json" -H "Csrf-Token: ${TOKEN}" "${OMADA_URL}/${CONTROLLER_ID}/api/v2/sites/default" | jq -r .result.id)"
# get the network name
NETWORK_NAME="$(curl -ks -b "/tmp/omada-cookies.txt" -H "Content-Type: application/json" -H "Csrf-Token: ${TOKEN}" "${OMADA_URL}/${CONTROLLER_ID}/api/v2/sites/${SITE_ID}/clients/${MAC_ADDRESS}" | jq -r .result.networkName)"
# get the dynamically assigned ip address (Using the same ip for simplicity - you might want to change this)
IP="$(curl -ks -b "/tmp/omada-cookies.txt" -H "Content-Type: application/json" -H "Csrf-Token: ${TOKEN}" "${OMADA_URL}/${CONTROLLER_ID}/api/v2/sites/${SITE_ID}/clients/${MAC_ADDRESS}" | jq -r .result.ip)"
# find the netId value
NET_ID="$(curl -ks -b "/tmp/omada-cookies.txt" -H "Content-Type: application/json" -H "Csrf-Token: ${TOKEN}" "${OMADA_URL}/${CONTROLLER_ID}/api/v2/sites/${SITE_ID}/setting/lan/networks/interface" | jq --arg NETWORK_NAME "$NETWORK_NAME" -c '.result.data[] | select(.name == $NETWORK_NAME)' | jq -r .id)"
# create a dhcp lease
curl -ks -b "/tmp/omada-cookies.txt" -H "Content-Type: application/json" -H "Csrf-Token: ${TOKEN}" "${OMADA_URL}/${CONTROLLER_ID}/api/v2/sites/${SITE_ID}/setting/service/dhcp" --data @<(cat <<EOF
{
"netId": "$NET_ID",
"mac": "$MAC_ADDRESS",
"ip": "$IP",
"description": "$DESCRIPTION",
"status": true
}
EOF
)
#!/bin/bash
# Example execution: delete_dhcp_reservation.sh C2-F3-D9-37-2A-33 https://omada.hosted:8043 username password
MAC_ADDRESS=$1
OMADA_URL=$2
USERNAME=$3
PASSWORD=$4
# 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)"
# get the default site id
SITE_ID="$(curl -ks -b "/tmp/omada-cookies.txt" -H "Content-Type: application/json" -H "Csrf-Token: ${TOKEN}" "${OMADA_URL}/${CONTROLLER_ID}/api/v2/sites/default" | jq -r .result.id)"
# delete a dhcp lease
curl -ks -b "/tmp/omada-cookies.txt" -X 'DELETE' -H "Content-Type: application/json" -H "Csrf-Token: ${TOKEN}" "${OMADA_URL}/${CONTROLLER_ID}/api/v2/sites/${SITE_ID}/setting/service/dhcp/${MAC_ADDRESS}"
@Naman1997
Copy link
Author

Special thanks to @mbentley for documenting the auth mechanism. Find the original gist here: https://gist.github.com/mbentley/03c198077c81d52cb029b825e9a6dc18

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