Generate MAC Address Range using PHP

Indeed, to generate MAC address range using PHP may be required in your next code. For instance, A MAC address is a unique identifier assigned to network interfaces for communications on a physical network segment. It is typically represented as a series of six pairs of hexadecimal digits separated by colons or hyphens. Thus, you can identify the first three pairs as the Organizationally Unique Identifier (OUI) and the last three pairs are the device identifier. For example, To generate all possible MAC addresses from a mac address OUI using PHP, you can use a loop.

Generate MAC Address Range

Of course, we are writing a PHP code example to generate MAC address from start and end mac address. In fact, you can read get first and end mac address from OUI using PHP, if you need that part. Our function will returns an array of all possible MAC addresses from the start and end mac address example. First, We used 00:11:22 OUI to crate first and end mac address in another post. Then, the PHP code example below to generate MAC address range.

PHP Code Example to generate MAC Address Range

$start_mac = 00:11:22:00:00:00;
$end_mac = 00:11:22:FF:FF:FF;

$start = hexdec(str_replace(':', '', $start_mac));
$end = hexdec(str_replace(':', '', $end_mac));

for ($i = $start; $i <= $end; $i++) {
    $mac = str_pad(dechex($i), 12, '0', STR_PAD_LEFT);
    $range[] = implode(':', str_split($mac, 2));
    //echo $mac . "\n";
}

return $range;

This code converts the starting and ending MAC addresses to hexadecimal integers and then iterates through all the integers in the given range. For each integer, it converts it back to a hexadecimal string representation of a MAC address, and then prints it out.

Note that this code assumes that the range given by $start_mac and $end_mac includes all valid MAC addresses, which is not always the case.

This code will generate all 16,777,216 possible MAC addresses within the range of 00:11:22:00:00:00 to 00:11:22:FF:FF:FF.

Share

You may also like...