RetreiveUnifiAPMAC.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/bin/bash
  2. # UniFi Controller credentials
  3. CONTROLLER_URL="https://your-controller-url:8443" # Replace with your Unifi Controller Domain name or IP:PORT
  4. USERNAME="your-username" # Replace with your Unifi Controller username
  5. PASSWORD="your-password" # Replace with your Unifi Controller password
  6. SITE_NAME="default" # Replace with your site name if different
  7. SSH_USERNAME="your-username" # Replace with your SSH username
  8. SSH_PASSWORD="your-password" # Replace with your SSH password
  9. # Function to retrieve site ID
  10. get_site_id() {
  11. SITES_RESPONSE=$(curl --silent --insecure --cookie /tmp/unifi_cookie \
  12. ${CONTROLLER_URL}/api/self/sites)
  13. if [[ $(echo "$SITES_RESPONSE" | jq -r '.meta.rc') != "ok" ]]; then
  14. echo "Failed to retrieve sites: $(echo "$SITES_RESPONSE" | jq -r '.meta.msg')" >&2
  15. exit 1
  16. fi
  17. SITE_ID=$(echo "$SITES_RESPONSE" | jq -r --arg site_name "$SITE_NAME" '.data[] | select(.desc == $site_name) | .name')
  18. if [[ -z "$SITE_ID" ]]; then
  19. echo "Site '$SITE_NAME' not found." >&2
  20. exit 1
  21. fi
  22. }
  23. # Function to get the 2.4GHz radio MAC address using sshpass
  24. get_radio_mac() {
  25. local AP_IP=$1
  26. # Determine the hardware architecture and set the interface name
  27. ARCHITECTURE=$(sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USERNAME@$AP_IP "uname -a")
  28. if echo "$ARCHITECTURE" | grep -q 'aarch64'; then
  29. INTERFACE='ra0'
  30. else
  31. INTERFACE='ath0'
  32. fi
  33. #echo "Using interface $INTERFACE for $AP_IP"
  34. if [ $INTERFACE = 'ra0' ]; then
  35. # Retrieve the MAC address for the determined interface
  36. MAC_ADDRESS=$(sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USERNAME@$AP_IP \
  37. "iwconfig $INTERFACE 2>/dev/null | grep 'Access Point' | awk '{print \$5}'")
  38. fi
  39. if [ $INTERFACE = 'ath0' ]; then
  40. # Retrieve the MAC address for the determined interface
  41. MAC_ADDRESS=$(sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USERNAME@$AP_IP \
  42. "iwconfig $INTERFACE 2>/dev/null | grep 'Access Point' | awk '{print \$6}'")
  43. fi
  44. echo "$MAC_ADDRESS"
  45. }
  46. # Login to UniFi Controller and get a session cookie
  47. LOGIN_RESPONSE=$(curl --silent --insecure --cookie-jar /tmp/unifi_cookie \
  48. --data "{\"username\":\"${USERNAME}\",\"password\":\"${PASSWORD}\"}" \
  49. --header 'Content-Type: application/json' \
  50. ${CONTROLLER_URL}/api/login)
  51. # Check if login was successful
  52. if [[ $(echo "$LOGIN_RESPONSE" | jq -r '.meta.rc') != "ok" ]]; then
  53. echo "Login failed: $(echo "$LOGIN_RESPONSE" | jq -r '.meta.msg')" >&2
  54. exit 1
  55. fi
  56. # Retrieve the correct site ID
  57. get_site_id
  58. # Get the list of devices and filter for WiFi access points
  59. DEVICE_LIST=$(curl --silent --insecure --cookie /tmp/unifi_cookie \
  60. ${CONTROLLER_URL}/api/s/${SITE_ID}/stat/device)
  61. # Check if device list retrieval was successful
  62. if [[ $(echo "$DEVICE_LIST" | jq -r '.meta.rc') != "ok" ]]; then
  63. echo "Failed to retrieve device list: $(echo "$DEVICE_LIST" | jq -r '.meta.msg')" >&2
  64. exit 1
  65. fi
  66. # Extract AP names and IP addresses
  67. AP_LIST=$(echo ${DEVICE_LIST} | jq -r '.data[] | select(.type=="uap") | "\(.name),\(.ip)"')
  68. # Output header
  69. echo "Access Point Name, 2.4GHz MAC Address"
  70. # Check if AP_LIST is empty and print a message if it is
  71. if [ -z "$AP_LIST" ]; then
  72. echo "No access points found or unable to retrieve access points. Please check your UniFi Controller settings."
  73. exit 1
  74. fi
  75. # Read AP names and IP addresses into an array
  76. IFS=$'\n' read -d '' -r -a ap_array <<< "$AP_LIST"
  77. # Loop through each AP, SSH to get the MAC address, and output the result
  78. for AP_INFO in "${ap_array[@]}"; do
  79. IFS=, read -r AP_NAME AP_IP <<< "$AP_INFO"
  80. #echo "Checking AP: $AP_NAME ($AP_IP)"
  81. MAC_ADDRESS=$(get_radio_mac $AP_IP)
  82. if [ -z "$MAC_ADDRESS" ]; then
  83. COUNT=$((COUNT + 1))
  84. echo "$COUNT"
  85. echo "Failed to retrieve MAC address for $AP_NAME ($AP_IP)"
  86. else
  87. COUNT=$((COUNT + 1))
  88. echo "$COUNT"
  89. echo "Unifi AP Name: $AP_NAME"
  90. #echo " $AP_IP"
  91. echo "MAC Address: $MAC_ADDRESS"
  92. echo ""
  93. fi
  94. done
  95. # Logout to clean up the session and suppress output
  96. curl --silent --insecure --cookie /tmp/unifi_cookie ${CONTROLLER_URL}/api/logout > /dev/null
  97. # Remove the cookie file
  98. rm /tmp/unifi_cookie