$oid_data = snmpwalk_cache_oid($device, $oid . '.' . $ip_version, $oid_data, 'IP-MIB', mib_dirs()); } // Rewrite IP-MIB array $ip_data = array(); foreach ($oid_data as $key => $entry) { if ($entry['ipAddressType'] == 'broadcast') { continue; } // Skip broadcasts $ip_address = str_replace($ip_version . '.', '', $key); $ifIndex = $entry['ipAddressIfIndex']; $entry['ipAddressPrefix'] = end(explode('.', $entry['ipAddressPrefix'])); if (!is_numeric($entry['ipAddressPrefix'])) { $entry['ipAddressPrefix'] = '32'; } if (is_ipv4_valid($ip_address, $entry['ipAddressPrefix']) !== FALSE) { foreach ($oids_ip as $oid) { $ip_data[$ifIndex][$ip_address][$oid] = $entry[$oid]; } } } if ($debug && $ip_data) { echo "IP-MIB\n"; print_vars($ip_data); } } // Caching old IPv4 addresses table $query = 'SELECT * FROM `ipv4_addresses` AS A LEFT JOIN `ports` AS I ON A.`port_id` = I.`port_id` WHERE I.`device_id` = ?'; foreach (dbFetchRows($query, array($device_id)) as $entry) {
} echo "IPv4 Addresses : "; $ip_version = 'ipv4'; // Get IP addresses from IP-MIB $oids_ip = array('ipAdEntIfIndex', 'ipAdEntNetMask'); //ipAdEntIfIndex.10.0.0.130 = 193 //ipAdEntNetMask.10.0.0.130 = 255.255.255.252 $oid_data = array(); foreach ($oids_ip as $oid) { $oid_data = snmpwalk_cache_oid($device, $oid, $oid_data, 'IP-MIB', mib_dirs()); } // Rewrite IP-MIB array $ip_data = array(); foreach ($oid_data as $ip_address => $entry) { $ifIndex = $entry['ipAdEntIfIndex']; if (is_ipv4_valid($ip_address, $entry['ipAdEntNetMask']) === FALSE) { continue; } $ip_data[$ifIndex][$ip_address] = $entry; } if ($debug && $ip_data) { echo "IP-MIB\n"; print_vars($ip_data); } // Caching old IPv4 addresses table $query = 'SELECT * FROM `ipv4_addresses` AS A LEFT JOIN `ports` AS I ON A.`port_id` = I.`port_id` WHERE I.`device_id` = ?'; foreach (dbFetchRows($query, array($device_id)) as $entry) { $old_table[$entry['ifIndex']][$entry['ipv4_address']] = $entry; }
if (!is_numeric($entry['prefix'])) { if (strlen($entry['mask'])) { // Yeah, passed empty prefix, but with correct mask $entry['prefix'] = $entry['mask']; } else { $entry['prefix'] = '32'; } } } else { $entry['prefix'] = $entry['mask']; } $ip_prefix = $entry['prefix']; $ip_origin = $entry['origin']; $ip_compressed = $ip_address; // just for compat with IPv6 if (is_ipv4_valid($ip_address, $ip_prefix) === FALSE) { print_debug("Address '{$ip_compressed}/{$ip_prefix}' skipped as invalid."); continue; } $addr = Net_IPv4::parseAddress($ip_address . '/' . $ip_prefix); $ip_cidr = $addr->bitmask; $ip_network = $addr->network . '/' . $ip_cidr; $full_address = $ip_address . '/' . $ip_cidr; $new_address = array('port_id' => $port_id, 'ipv4_address' => $ip_address, 'ipv4_prefixlen' => $ip_cidr); } else { if (!is_numeric($entry['prefix'])) { $entry['prefix'] = '128'; } $ip_prefix = $entry['prefix']; $ip_origin = $entry['origin']; $ip_compressed = Net_IPv6::compress($ip_address, TRUE);