112 lines
4 KiB
PHP
112 lines
4 KiB
PHP
<?php
|
|
|
|
error_reporting(0);
|
|
|
|
// Ange IP-range av switchar
|
|
$switch_ips = array(
|
|
'172.30.0.20',
|
|
'172.30.0.21',
|
|
'172.30.0.22',
|
|
'172.30.0.23',
|
|
'172.30.0.24',
|
|
'172.30.0.25',
|
|
'172.30.0.26',
|
|
'172.30.0.27',
|
|
'172.30.0.28',
|
|
'172.30.0.28'
|
|
);
|
|
$community = 'public'; // Byt ut mot ditt SNMP-community
|
|
|
|
foreach ($switch_ips as $switch_ip) {
|
|
echo "Bearbetar switch: $switch_ip\n";
|
|
|
|
// 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);
|
|
if ($dot1dBasePortIfIndex_oids === false) {
|
|
echo "Kunde inte hämta data från switchen $switch_ip\n\n";
|
|
continue;
|
|
}
|
|
|
|
$bridgePortToIfIndex = array();
|
|
foreach ($dot1dBasePortIfIndex_oids as $oid => $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";
|
|
}
|
|
?>
|
|
|