RetreiveUnifiAPMAC.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/usr/bin/env bash
  2. # This script depends on the following applications:
  3. # bash
  4. # sshpass
  5. # jq
  6. # curl
  7. # UniFi Controller credentials
  8. CONTROLLER_URL="https://your-controller-url:8443" # Replace with your Unifi Controller Domain name or IP:PORT
  9. USERNAME="your-username" # Replace with your Unifi Controller username
  10. PASSWORD="your-password" # Replace with your Unifi Controller password
  11. SITE_NAME="default" # Replace with your site name if different
  12. SSH_USERNAME="your-username" # Replace with your SSH username
  13. SSH_PASSWORD="your-password" # Replace with your SSH password
  14. #########################################################################################################
  15. ###### Do not edit below this, unless you know what you are doing. :-) #################################
  16. ##### Script made by Dennis Juhler Aagaard #################################
  17. ##### License is BSD 0-Clause #################################
  18. # Copyright (C) 2024 by Dennis Juhler Aagaard <dja@radixadm.dk> #################################
  19. # Permission to use, copy, modify, and/or distribute this software for #################################
  20. # any purpose with or without fee is hereby granted. #################################
  21. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL #################################
  22. # WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED #################################
  23. # WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL #################################
  24. # THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR #################################
  25. # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS #################################
  26. # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE #################################
  27. # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH #################################
  28. # THE USE OR PERFORMANCE OF THIS SOFTWARE #################################
  29. #########################################################################################################
  30. if command -v sshpass &> /dev/null; then
  31. echo "sshpass is installed"
  32. else
  33. echo "sshpass is not installed."
  34. echo "install it by running your favourite pkg manager."
  35. exit 1
  36. fi
  37. if command -v jq &> /dev/null; then
  38. echo "jq is installed"
  39. else
  40. echo "jq is not installed."
  41. echo "install it by running your favourite pkg manager."
  42. exit 1
  43. fi
  44. if command -v curl &> /dev/null; then
  45. echo "curl is installed"
  46. else
  47. echo "curl is not installed."
  48. echo "install it by running your favourite pkg manager."
  49. exit 1
  50. fi
  51. echo ""
  52. # Function to retrieve site ID
  53. get_site_id() {
  54. SITES_RESPONSE=$(curl --silent --insecure --cookie /tmp/unifi_cookie \
  55. ${CONTROLLER_URL}/api/self/sites)
  56. if [[ $(echo "$SITES_RESPONSE" | jq -r '.meta.rc') != "ok" ]]; then
  57. echo "Failed to retrieve sites: $(echo "$SITES_RESPONSE" | jq -r '.meta.msg')" >&2
  58. exit 1
  59. fi
  60. SITE_ID=$(echo "$SITES_RESPONSE" | jq -r --arg site_name "$SITE_NAME" '.data[] | select(.desc == $site_name) | .name')
  61. if [[ -z "$SITE_ID" ]]; then
  62. echo "Site '$SITE_NAME' not found." >&2
  63. exit 1
  64. fi
  65. }
  66. # Function to get the 2.4GHz radio MAC address using sshpass
  67. get_radio_mac() {
  68. local AP_IP=$1
  69. # Determine the hardware architecture and set the interface name
  70. ARCHITECTURE=$(sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USERNAME@$AP_IP "uname -a")
  71. if echo "$ARCHITECTURE" | grep -q 'aarch64'; then
  72. INTERFACE='ra0'
  73. else
  74. INTERFACE='ath0'
  75. fi
  76. #echo "Using interface $INTERFACE for $AP_IP"
  77. if [ $INTERFACE = 'ra0' ]; then
  78. # Retrieve the MAC address for the determined interface
  79. MAC_ADDRESS=$(sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USERNAME@$AP_IP \
  80. "iwconfig $INTERFACE 2>/dev/null | grep 'Access Point' | awk '{print \$5}'")
  81. fi
  82. if [ $INTERFACE = 'ath0' ]; then
  83. # Retrieve the MAC address for the determined interface
  84. MAC_ADDRESS=$(sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USERNAME@$AP_IP \
  85. "iwconfig $INTERFACE 2>/dev/null | grep 'Access Point' | awk '{print \$6}'")
  86. fi
  87. echo "$MAC_ADDRESS"
  88. }
  89. # Login to UniFi Controller and get a session cookie
  90. LOGIN_RESPONSE=$(curl --silent --insecure --cookie-jar /tmp/unifi_cookie \
  91. --data "{\"username\":\"${USERNAME}\",\"password\":\"${PASSWORD}\"}" \
  92. --header 'Content-Type: application/json' \
  93. ${CONTROLLER_URL}/api/login)
  94. # Check if login was successful
  95. if [[ $(echo "$LOGIN_RESPONSE" | jq -r '.meta.rc') != "ok" ]]; then
  96. echo "Login failed: $(echo "$LOGIN_RESPONSE" | jq -r '.meta.msg')" >&2
  97. exit 1
  98. fi
  99. # Retrieve the correct site ID
  100. get_site_id
  101. # Get the list of devices and filter for WiFi access points
  102. DEVICE_LIST=$(curl --silent --insecure --cookie /tmp/unifi_cookie \
  103. ${CONTROLLER_URL}/api/s/${SITE_ID}/stat/device)
  104. # Check if device list retrieval was successful
  105. if [[ $(echo "$DEVICE_LIST" | jq -r '.meta.rc') != "ok" ]]; then
  106. echo "Failed to retrieve device list: $(echo "$DEVICE_LIST" | jq -r '.meta.msg')" >&2
  107. exit 1
  108. fi
  109. # Extract AP names and IP addresses
  110. AP_LIST=$(echo ${DEVICE_LIST} | jq -r '.data[] | select(.type=="uap") | "\(.name),\(.ip)"')
  111. # Output header
  112. echo "Access Point Name, 2.4GHz MAC Address"
  113. echo ""
  114. # Check if AP_LIST is empty and print a message if it is
  115. if [ -z "$AP_LIST" ]; then
  116. echo "No access points found or unable to retrieve access points. Please check your UniFi Controller settings."
  117. exit 1
  118. fi
  119. # Read AP names and IP addresses into an array
  120. IFS=$'\n' read -d '' -r -a ap_array <<< "$AP_LIST"
  121. # Loop through each AP, SSH to get the MAC address, and output the result
  122. for AP_INFO in "${ap_array[@]}"; do
  123. IFS=, read -r AP_NAME AP_IP <<< "$AP_INFO"
  124. #echo "Checking AP: $AP_NAME ($AP_IP)"
  125. MAC_ADDRESS=$(get_radio_mac $AP_IP)
  126. if [ -z "$MAC_ADDRESS" ]; then
  127. COUNT=$((COUNT + 1))
  128. echo "$COUNT"
  129. echo "Failed to retrieve MAC address for $AP_NAME ($AP_IP)"
  130. else
  131. COUNT=$((COUNT + 1))
  132. echo "$COUNT"
  133. echo "Unifi AP Name: $AP_NAME"
  134. #echo " $AP_IP"
  135. echo "MAC Address: $MAC_ADDRESS"
  136. echo ""
  137. fi
  138. done
  139. # Logout to clean up the session and suppress output
  140. curl --silent --insecure --cookie /tmp/unifi_cookie ${CONTROLLER_URL}/api/logout > /dev/null
  141. # Remove the cookie file
  142. rm /tmp/unifi_cookie