Determining Vendor information from MAC Addresses
- March 31st, 2009
- Posted in Code
- By Z-95
- Write comment
For my ongoing AP code work that I am doing, I wanted to supply AP vendors based on the MAC address of the AP. Now, for any cloned MAC addresses this will be inaccurate or give an Unknown entry, but for any genuine MAC address the vendor can be determined!
I first started with the OUI file published by the IEEE: http://standards.ieee.org/regauth/oui/oui.txt This file gives the vendor information for the leading 3 bytes of the 6 byte MAC address. Each leading 3 bytes is uniquely assigned to one vendor, which can hold 2^24 or 16.8 million unique MAC addresses.
To begin, I ran
grep "(hex)" oui.txt>ouitab.txt
to get just the lines with the leading half of the mac in the form XX-XX-XX and the vendor’s name. I then replaced the spaces, (hex), and one tab to get all lines in the form XX-XX-XX(tab)Vendor and saved this as oui.txt.
Instead of creating a new field in the geoinfo database from GPSDrive, I instead chose to use the unused comment field to hold the vendor information. The rest was simple: write a php script to search the revised oui.txt file for the leading 3 bytes of the MAC for each AP in the database and then update the database by writing the Vendor to the comment field. I came up with this:
<?php
$oui = file('oui.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$dbconnection = mysql_connect('localhost', 'root', 'Insert Password Here');
$dbselected = mysql_select_db('geoinfo', $dbconnection);
$result = mysql_query("SELECT * FROM waypoints");
while ($row = mysql_fetch_assoc($result)) {
$mac = $row['macaddr'];
$macorig = $row['macaddr'];
$mac = substr($mac, 0, 8);
$mac = str_replace(":", "-", $mac);
$mac = strtoupper($mac);
$search = preg_grep("/{$mac}/i", $oui);
$search = array_values($search);
$exploded = explode("\t", $search[0]);
if ($exploded[1] == "")
$update = "UPDATE waypoints SET comment = 'Unknown' WHERE macaddr = '$macorig'";
else
$update = "UPDATE waypoints SET comment = '$exploded[1]' WHERE macaddr = '$macorig'";
$updateresult = mysql_query($update);
}
mysql_close($dbconnection);
echo "Complete";
?>
This set the comment field with the found vendor name if it could be found or put Unknown if the MAC could not be found.
Note that using preg_grep in this way is VERY slow as it linearly searches oui.txt for a match for EACH AP. For ~3700 APs it took about 45 seconds to run, which was fine for a quick and dirty script but for more APs would of course take longer.
More code updates to come as I work more on the AP code!
No comments yet.