Update get_mac_list.php

This commit is contained in:
heiti 2024-10-28 22:46:03 +01:00
parent f642b61bcf
commit da8e8e4ca2

View file

@ -1,112 +1,112 @@
<?php <?php
error_reporting(0); error_reporting(0); // skippa felmeddelanden
// Ange IP-range av switchar // Ange IP-range av switchar
$switch_ips = array( $switch_ips = array(
'172.30.0.20', '172.30.0.20',
'172.30.0.21', '172.30.0.21',
'172.30.0.22', '172.30.0.22',
'172.30.0.23', '172.30.0.23',
'172.30.0.24', '172.30.0.24',
'172.30.0.25', '172.30.0.25',
'172.30.0.26', '172.30.0.26',
'172.30.0.27', '172.30.0.27',
'172.30.0.28', '172.30.0.28',
'172.30.0.28' '172.30.0.28'
); );
$community = 'public'; // Byt ut mot ditt SNMP-community $community = 'public'; // SNMP "lösenord"
foreach ($switch_ips as $switch_ip) { foreach ($switch_ips as $switch_ip) {
echo "Bearbetar switch: $switch_ip\n"; echo "Bearbetar switch: $switch_ip\n";
// Hämta mappningen av bridge-portar till interface-index: dot1dBasePortIfIndex // Hämta mappningen av bridge-portar till interface-index: dot1dBasePortIfIndex
$dot1dBasePortIfIndex_oids = @snmp2_real_walk($switch_ip, $community, '1.3.6.1.2.1.17.1.4.1.2',-1,-1); $dot1dBasePortIfIndex_oids = @snmp2_real_walk($switch_ip, $community, '1.3.6.1.2.1.17.1.4.1.2',-1,-1);
if ($dot1dBasePortIfIndex_oids === false) { if ($dot1dBasePortIfIndex_oids === false) {
echo "Kunde inte hämta data från switchen $switch_ip\n\n"; echo "Kunde inte hämta data från switchen $switch_ip\n\n";
continue; continue;
} }
$bridgePortToIfIndex = array(); $bridgePortToIfIndex = array();
foreach ($dot1dBasePortIfIndex_oids as $oid => $value) { foreach ($dot1dBasePortIfIndex_oids as $oid => $value) {
// Extrahera bridge-portnummer från OID // Extrahera bridge-portnummer från OID
$parts = explode('.', $oid); $parts = explode('.', $oid);
$bridgePort = array_pop($parts); $bridgePort = array_pop($parts);
// Rensa värdet // Rensa värdet
$ifIndex = trim(str_replace('INTEGER:', '', $value)); $ifIndex = trim(str_replace('INTEGER:', '', $value));
$bridgePortToIfIndex[$bridgePort] = $ifIndex; $bridgePortToIfIndex[$bridgePort] = $ifIndex;
} }
// Hämta interface-namn: ifDescr // 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); $ifDescr_oids = snmp2_real_walk($switch_ip, $community, '1.3.6.1.2.1.2.2.1.2',-1,-1);
$ifIndexToName = array(); $ifIndexToName = array();
foreach ($ifDescr_oids as $oid => $value) { foreach ($ifDescr_oids as $oid => $value) {
// Extrahera ifIndex från OID // Extrahera ifIndex från OID
$parts = explode('.', $oid); $parts = explode('.', $oid);
$ifIndex = array_pop($parts); $ifIndex = array_pop($parts);
// Rensa värdet // Rensa värdet
$ifName = trim(str_replace('STRING:', '', $value), '" '); $ifName = trim(str_replace('STRING:', '', $value), '" ');
$ifIndexToName[$ifIndex] = $ifName; $ifIndexToName[$ifIndex] = $ifName;
} }
// Hämta MAC-adresser: dot1dTpFdbAddress // 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); $dot1dTpFdbAddress_oids = snmp2_real_walk($switch_ip, $community, '1.3.6.1.2.1.17.4.3.1.1',-1,-1);
$macAddresses = array(); $macAddresses = array();
foreach ($dot1dTpFdbAddress_oids as $oid => $value) { foreach ($dot1dTpFdbAddress_oids as $oid => $value) {
// Extrahera index från OID // Extrahera index från OID
$parts = explode('.', $oid); $parts = explode('.', $oid);
$indexParts = array_slice($parts, 11); // OID-prefixet är 11 delar $indexParts = array_slice($parts, 11); // OID-prefixet är 11 delar
$index = implode('.', $indexParts); $index = implode('.', $indexParts);
// Rensa och formatera MAC-adressen // Rensa och formatera MAC-adressen
$macRaw = trim(str_replace('Hex-STRING: ', '', $value)); $macRaw = trim(str_replace('Hex-STRING: ', '', $value));
$macAddress = strtolower(str_replace(' ', ':', $macRaw)); $macAddress = strtolower(str_replace(' ', ':', $macRaw));
$macAddresses[$index] = $macAddress; $macAddresses[$index] = $macAddress;
} }
// Hämta bridge-portar för varje MAC-adress: dot1dTpFdbPort // 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); $dot1dTpFdbPort_oids = snmp2_real_walk($switch_ip, $community, '1.3.6.1.2.1.17.4.3.1.2',-1,-1);
$bridgePorts = array(); $bridgePorts = array();
foreach ($dot1dTpFdbPort_oids as $oid => $value) { foreach ($dot1dTpFdbPort_oids as $oid => $value) {
// Extrahera index från OID // Extrahera index från OID
$parts = explode('.', $oid); $parts = explode('.', $oid);
$indexParts = array_slice($parts, 11); // OID-prefixet är 11 delar $indexParts = array_slice($parts, 11); // OID-prefixet är 11 delar
$index = implode('.', $indexParts); $index = implode('.', $indexParts);
$bridgePort = trim(str_replace('INTEGER:', '', $value)); $bridgePort = trim(str_replace('INTEGER:', '', $value));
$bridgePorts[$index] = $bridgePort; $bridgePorts[$index] = $bridgePort;
} }
// Mappa MAC-adresser till interface // Mappa MAC-adresser till interface
$macToInterface = array(); $macToInterface = array();
foreach ($macAddresses as $index => $macAddress) { foreach ($macAddresses as $index => $macAddress) {
if (isset($bridgePorts[$index])) { if (isset($bridgePorts[$index])) {
$bridgePort = $bridgePorts[$index]; $bridgePort = $bridgePorts[$index];
if ($bridgePort == '0') { if ($bridgePort == '0') {
// Bridge-port ID 0 indikerar switchens interna MAC-adress // Bridge-port ID 0 indikerar switchens interna MAC-adress
$macToInterface[$macAddress] = 'Intern port'; $macToInterface[$macAddress] = 'Switchens interna mac-adress';
} elseif (isset($bridgePortToIfIndex[$bridgePort])) { } elseif (isset($bridgePortToIfIndex[$bridgePort])) {
$ifIndex = $bridgePortToIfIndex[$bridgePort]; $ifIndex = $bridgePortToIfIndex[$bridgePort];
if (isset($ifIndexToName[$ifIndex])) { if (isset($ifIndexToName[$ifIndex])) {
$interfaceName = $ifIndexToName[$ifIndex]; $interfaceName = $ifIndexToName[$ifIndex];
$macToInterface[$macAddress] = $interfaceName; $macToInterface[$macAddress] = $interfaceName;
} else { } else {
$macToInterface[$macAddress] = 'Okänt interface'; $macToInterface[$macAddress] = 'Okänt interface';
} }
} else { } else {
$macToInterface[$macAddress] = 'Okänd bridge-port'; $macToInterface[$macAddress] = 'Okänd switch-port';
} }
} else { } else {
$macToInterface[$macAddress] = 'Okänd bridge-port'; $macToInterface[$macAddress] = 'Okänd switch-port';
} }
} }
// Skriv ut listan för varje switch // Skriv ut listan för varje switch
foreach ($macToInterface as $macAddress => $interfaceName) { foreach ($macToInterface as $macAddress => $interfaceName) {
echo "MAC-adress: $macAddress - Port: $interfaceName\n"; echo "MAC-adress: $macAddress - Switch Port: $interfaceName\n";
} }
echo "\n"; echo "\n";
} }
?> ?>