EN 
06.12.2025 Mikuláš WELCOME IN MY WORLD

This website is originally written in the Czech language. Most content is machine (AI) translated into English. The translation may not be exact and may contain errors.

Tento článek si můžete zobrazit v originální české verzi. You can view this article in the original Czech version.
Stažení ARP tabulky z routeru (switche) pomocí SNMP a PHP

Download ARP table from router (switch) using SNMP and PHP

| Petr Bouška - Samuraj |
Having found the MAC addresses of the devices that are connected to the ports of the switch, it would be useful to supplement these MAC addresses with the corresponding IP addresses, which will tell us more about the device. If we have a central router (switch) in the network through which individual computers communicate (on L3), we can download its ARP table and that's it.
displayed: 16 568x (15 919 CZ, 649 EN) | Comments [0]

Routers operate at the third layer (OSI model layer 3), using IP addresses to determine where to send data. However, to deliver data to a directly connected subnet, it also needs to know the MAC address of the station. Therefore, it maintains an ARP table (Address Resolution Protocol) or, in other words, a translation table for mapping IP addresses to physical addresses. The entries in this table are only stored for a limited time.

We can download this table using SNMP and use it to find the current IP addresses corresponding to MAC addresses. This time, it is not a complicated operation, except that there are several possible OIDs where these values are located.

1. MIB file IP-MIB, OID = 1.3.6.1.2.1.4.22, which is .iso.org.dod.internet.mgmt.mib-2.ip.ipNetToMediaTable

Name OID Description
ipNetToMediaPhysAddress .1.3.6.1.2.1.4.22.1.2 MAC address
ipNetToMediaNetAddress .1.3.6.1.2.1.4.22.1.3 IP address
ipNetToMediaType .1.3.6.1.2.1.4.22.1.4 mapping type, 3 - dynamic

2. MIB file RFC1213-MIB, OID = 1.3.6.1.2.1.3.1, which is .iso.org.dod.internet.mgmt.mib-2.at.atTable

Name OID Description
atPhysAddress .1.3.6.1.2.1.3.1.1.2 MAC address
atNetAddress .1.3.6.1.2.1.3.1.1.3 IP address

3. MIB file IP-MIB, OID = 1.3.6.1.2.1.4.35, which is .iso.org.dod.internet.mgmt.mib-2.ip.ipNetToPhysicalTable

Name OID Description
ipNetToPhysicalNetAddress .1.3.6.1.2.1.4.35.1.3 IP address
ipNetToPhysicalPhysAddress .1.3.6.1.2.1.4.35.1.4 MAC address

The documentation states that version 1 has been replaced by the more universal version 3 (which, by the way, also contains a number of other values). However, on Catalyst 3750 and 2960 switches, I was only able to get versions 1 and 2 to work.

If we use version 2 (or identically 1), we get the following output:

[at.atTable.atEntry.atPhysAddress.25.1.192.168.100.2] => Hex: 00 1E 49 8F 3C 00 

This is a value that contains the MAC address. At the same time, it can be seen that the last four digits of the OID are the IP address to which the given MAC belongs. The same index is used for the value that returns the IP address. I couldn't find a description anywhere, but in practice, it always works this way, so I simplified the code, as shown below.

Implementation in PHP

The following code is just a fragment that builds on the code in the previous parts. The `getARP` function loads only the MAC address values and creates an array where the index is the MAC address and the IP address is taken from the OID index. Instead of a more complex method where an indexed array would be created and each value would contain IP and MAC. Another simplification is in the search.

function getARP(&$arp, $ip, $comm) {
  $array  = snmprealwalk($ip, $comm, ".1.3.6.1.2.1.4.22.1.2");
  foreach($array  as $key => $val) {
    $ip = ereg_replace('.*\.([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$', "\\1", $key);  
    $mac = format_snmp_string($val);
    $arp[$mac] = $ip;
  }
}

We only run the `getARP` function once at the beginning. Probably we have only one router, although it can also be used for multiple ones. Then we go through the individual switches and after loading the MAC addresses, we run the `assignIPtoMAC` function, which goes through all the interfaces and if a MAC address is loaded on one of them, it returns the value from the `$arp` array based on this MAC address.

function assignIPtoMAC(&$interfaces, $name) {
  global $arp;
  foreach($interfaces as $key => $val) 
    if(empty($val["MAC"])) $interfaces[$key][$name] = "";
    else $interfaces[$key][$name] = $arp[$val["MAC"]];
}
Author:

Related articles:

SNMP

Simple Network Management Protocol (SNMP) is very useful for managing a computer network.

If you want write something about this article use comments.

Comments

There are no comments yet.

Add comment

Insert tag: strong em link

Help:
  • maximum length of comment is 2000 characters
  • HTML tags are not allowed (they will be removed), you can use only the special tags listed above the input field
  • new line (ENTER) ends paragraph and start new one
  • when you respond to a comment, put the original comment number in squar brackets at the beginning of the paragraph (line)