diff --git a/get_mac_list.php b/get_mac_list.php new file mode 100644 index 0000000..23e2241 --- /dev/null +++ b/get_mac_list.php @@ -0,0 +1,112 @@ + $value) { + // Extrahera bridge-portnummer från OID + $parts = explode('.', $oid); + $bridgePort = array_pop($parts); + // Rensa värdet + $ifIndex = trim(str_replace('INTEGER:', '', $value)); + $bridgePortToIfIndex[$bridgePort] = $ifIndex; + } + + // Hämta interface-namn: ifDescr + $ifDescr_oids = snmp2_real_walk($switch_ip, $community, '1.3.6.1.2.1.2.2.1.2',-1,-1); + + $ifIndexToName = array(); + foreach ($ifDescr_oids as $oid => $value) { + // Extrahera ifIndex från OID + $parts = explode('.', $oid); + $ifIndex = array_pop($parts); + // Rensa värdet + $ifName = trim(str_replace('STRING:', '', $value), '" '); + $ifIndexToName[$ifIndex] = $ifName; + } + + // Hämta MAC-adresser: dot1dTpFdbAddress + $dot1dTpFdbAddress_oids = snmp2_real_walk($switch_ip, $community, '1.3.6.1.2.1.17.4.3.1.1',-1,-1); + + $macAddresses = array(); + foreach ($dot1dTpFdbAddress_oids as $oid => $value) { + // Extrahera index från OID + $parts = explode('.', $oid); + $indexParts = array_slice($parts, 11); // OID-prefixet är 11 delar + $index = implode('.', $indexParts); + // Rensa och formatera MAC-adressen + $macRaw = trim(str_replace('Hex-STRING: ', '', $value)); + $macAddress = strtolower(str_replace(' ', ':', $macRaw)); + $macAddresses[$index] = $macAddress; + } + + // Hämta bridge-portar för varje MAC-adress: dot1dTpFdbPort + $dot1dTpFdbPort_oids = snmp2_real_walk($switch_ip, $community, '1.3.6.1.2.1.17.4.3.1.2',-1,-1); + + $bridgePorts = array(); + foreach ($dot1dTpFdbPort_oids as $oid => $value) { + // Extrahera index från OID + $parts = explode('.', $oid); + $indexParts = array_slice($parts, 11); // OID-prefixet är 11 delar + $index = implode('.', $indexParts); + $bridgePort = trim(str_replace('INTEGER:', '', $value)); + $bridgePorts[$index] = $bridgePort; + } + + // Mappa MAC-adresser till interface + $macToInterface = array(); + + foreach ($macAddresses as $index => $macAddress) { + if (isset($bridgePorts[$index])) { + $bridgePort = $bridgePorts[$index]; + if ($bridgePort == '0') { + // Bridge-port ID 0 indikerar switchens interna MAC-adress + $macToInterface[$macAddress] = 'Intern port'; + } elseif (isset($bridgePortToIfIndex[$bridgePort])) { + $ifIndex = $bridgePortToIfIndex[$bridgePort]; + if (isset($ifIndexToName[$ifIndex])) { + $interfaceName = $ifIndexToName[$ifIndex]; + $macToInterface[$macAddress] = $interfaceName; + } else { + $macToInterface[$macAddress] = 'Okänt interface'; + } + } else { + $macToInterface[$macAddress] = 'Okänd bridge-port'; + } + } else { + $macToInterface[$macAddress] = 'Okänd bridge-port'; + } + } + // Skriv ut listan för varje switch + foreach ($macToInterface as $macAddress => $interfaceName) { + echo "MAC-adress: $macAddress - Port: $interfaceName\n"; + } + echo "\n"; +} +?> +