/unifi-probe.sh Secret
Created
April 10, 2025 19:30
Unifi network probe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SWITCH_IP="$1" | |
SSH_OPTS="-o Tunnel=no -o PermitLocalCommand=no -o ControlPath=none" | |
mac_output=$(ssh $SSH_OPTS "$SWITCH_IP" 'swctrl mac show') | |
mca_output=$(ssh $SSH_OPTS "$SWITCH_IP" 'mca-dump') | |
poe_output=$(ssh $SSH_OPTS "$SWITCH_IP" 'swctrl poe show') | |
lldp_output=$(ssh $SSH_OPTS "$SWITCH_IP" 'swctrl lldp show') | |
printf "%-6s %-20s %-25s %-18s %-15s %-10s %-25s\n" "Port" "Port Name" "Device Name" "MAC Address" "IP Address" "PoE (W)" "Neighbor" | |
printf "%s\n" "-----------------------------------------------------------------------------------------------------------------------------------------------------" | |
mac_lines=$(echo "$mac_output" | awk 'NR>2 && /^[[:space:]]*[0-9]+/') | |
while read -r line; do | |
port=$(echo "$line" | awk '{print $1}') | |
mac=$(echo "$line" | awk '{print $3}') | |
ip=$(echo "$line" | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}' | head -n1) | |
[ -z "$ip" ] && ip="-" | |
device_name=$(echo "$line" | awk '{ if (NF>=5 && $5 !~ /^[0-9]+$/) print $5; else print "-" }') | |
[ "$device_name" = "(no name)" ] && device_name="-" | |
port_name=$(echo "$mca_output" | jq -r '.lldp_table[] | select(.local_port_idx=='"$port"') | .local_port_name' | head -n1) | |
[ -z "$port_name" ] && port_name="-" | |
trunk_check=$(echo "$mca_output" | jq -r '.port_table[] | select((.is_uplink==true or .lag_member==true) and .port_idx=='"$port"') | .port_idx' | head -n1) | |
port_label="$port" | |
[ ! -z "$trunk_check" ] && port_label="${port}t" | |
watts=$(echo "$poe_output" | awk -v p="$port" '$1==p {print $(NF-2)}' | head -n1) | |
[ -z "$watts" ] && watts="-" | |
neighbor=$(echo "$lldp_output" | awk -v p="$port" 'NR>2 { if ($1==p) {print $4 " (" $2 ")"; exit } }') | |
[ -z "$neighbor" ] && neighbor="-" | |
printf "%-6s %-20s %-25s %-18s %-15s %-10s %-25s\n" "$port_label" "$port_name" "$device_name" "$mac" "$ip" "$watts" "$neighbor" | |
done <<< "$mac_lines" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment