| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- #!/bin/bash
- # UniFi Controller credentials
- CONTROLLER_URL="https://your-controller-url:8443" # Replace with your Unifi Controller Domain name or IP:PORT
- USERNAME="your-username" # Replace with your Unifi Controller username
- PASSWORD="your-password" # Replace with your Unifi Controller password
- SITE_NAME="default" # Replace with your site name if different
- SSH_USERNAME="your-username" # Replace with your SSH username
- SSH_PASSWORD="your-password" # Replace with your SSH password
- # Function to retrieve site ID
- get_site_id() {
- SITES_RESPONSE=$(curl --silent --insecure --cookie /tmp/unifi_cookie \
- ${CONTROLLER_URL}/api/self/sites)
-
- if [[ $(echo "$SITES_RESPONSE" | jq -r '.meta.rc') != "ok" ]]; then
- echo "Failed to retrieve sites: $(echo "$SITES_RESPONSE" | jq -r '.meta.msg')" >&2
- exit 1
- fi
- SITE_ID=$(echo "$SITES_RESPONSE" | jq -r --arg site_name "$SITE_NAME" '.data[] | select(.desc == $site_name) | .name')
- if [[ -z "$SITE_ID" ]]; then
- echo "Site '$SITE_NAME' not found." >&2
- exit 1
- fi
- }
- # Function to get the 2.4GHz radio MAC address using sshpass
- get_radio_mac() {
- local AP_IP=$1
- # Determine the hardware architecture and set the interface name
- ARCHITECTURE=$(sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USERNAME@$AP_IP "uname -a")
- if echo "$ARCHITECTURE" | grep -q 'aarch64'; then
- INTERFACE='ra0'
- else
- INTERFACE='ath0'
- fi
- #echo "Using interface $INTERFACE for $AP_IP"
- if [ $INTERFACE = 'ra0' ]; then
- # Retrieve the MAC address for the determined interface
- MAC_ADDRESS=$(sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USERNAME@$AP_IP \
- "iwconfig $INTERFACE 2>/dev/null | grep 'Access Point' | awk '{print \$5}'")
- fi
- if [ $INTERFACE = 'ath0' ]; then
- # Retrieve the MAC address for the determined interface
- MAC_ADDRESS=$(sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USERNAME@$AP_IP \
- "iwconfig $INTERFACE 2>/dev/null | grep 'Access Point' | awk '{print \$6}'")
- fi
- echo "$MAC_ADDRESS"
- }
- # Login to UniFi Controller and get a session cookie
- LOGIN_RESPONSE=$(curl --silent --insecure --cookie-jar /tmp/unifi_cookie \
- --data "{\"username\":\"${USERNAME}\",\"password\":\"${PASSWORD}\"}" \
- --header 'Content-Type: application/json' \
- ${CONTROLLER_URL}/api/login)
- # Check if login was successful
- if [[ $(echo "$LOGIN_RESPONSE" | jq -r '.meta.rc') != "ok" ]]; then
- echo "Login failed: $(echo "$LOGIN_RESPONSE" | jq -r '.meta.msg')" >&2
- exit 1
- fi
- # Retrieve the correct site ID
- get_site_id
- # Get the list of devices and filter for WiFi access points
- DEVICE_LIST=$(curl --silent --insecure --cookie /tmp/unifi_cookie \
- ${CONTROLLER_URL}/api/s/${SITE_ID}/stat/device)
- # Check if device list retrieval was successful
- if [[ $(echo "$DEVICE_LIST" | jq -r '.meta.rc') != "ok" ]]; then
- echo "Failed to retrieve device list: $(echo "$DEVICE_LIST" | jq -r '.meta.msg')" >&2
- exit 1
- fi
- # Extract AP names and IP addresses
- AP_LIST=$(echo ${DEVICE_LIST} | jq -r '.data[] | select(.type=="uap") | "\(.name),\(.ip)"')
- # Output header
- echo "Access Point Name, 2.4GHz MAC Address"
- # Check if AP_LIST is empty and print a message if it is
- if [ -z "$AP_LIST" ]; then
- echo "No access points found or unable to retrieve access points. Please check your UniFi Controller settings."
- exit 1
- fi
- # Read AP names and IP addresses into an array
- IFS=$'\n' read -d '' -r -a ap_array <<< "$AP_LIST"
- # Loop through each AP, SSH to get the MAC address, and output the result
- for AP_INFO in "${ap_array[@]}"; do
- IFS=, read -r AP_NAME AP_IP <<< "$AP_INFO"
-
- #echo "Checking AP: $AP_NAME ($AP_IP)"
- MAC_ADDRESS=$(get_radio_mac $AP_IP)
- if [ -z "$MAC_ADDRESS" ]; then
- COUNT=$((COUNT + 1))
- echo "$COUNT"
- echo "Failed to retrieve MAC address for $AP_NAME ($AP_IP)"
-
- else
- COUNT=$((COUNT + 1))
- echo "$COUNT"
- echo "Unifi AP Name: $AP_NAME"
- #echo " $AP_IP"
- echo "MAC Address: $MAC_ADDRESS"
- echo ""
-
- fi
- done
- # Logout to clean up the session and suppress output
- curl --silent --insecure --cookie /tmp/unifi_cookie ${CONTROLLER_URL}/api/logout > /dev/null
- # Remove the cookie file
- rm /tmp/unifi_cookie
|