Parser/ Importer for the IEEE OUI List

In a distant past I created a vendor lookoup page for MAC addresses, that according to my Google Analytics is still being used from all over the world. Wow.

Most of the information in this tool was pretty outdated, because I had imported the IEEE oui.txt file in a SQLite database by hand and was too lazy to do that again.

Well, today I found some old Perl code back that I started on to automate this process. I never managed to get that to work in the past, somehow it worked after only a few edits today.

I quickly added some SQLite Perl DBI stuff, and suddenly the database for mac2vendor is up to date again.

From now on this script will run as a cronjob from time to time to keep mac2vendor more up to date.

Below you can find my quickly thrown together Perl to parse oui.txt and to insert it in a database. Many things can be improved on this quick hack, but well, it might be useful for someone:

(oui_import.pl) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/perl -w
# quick and dirty importer for the IEEE oui.txt file
# Hessel Schut, hessel@sigint.cc, 2010-08-07

use strict;
use LWP;

use DBI;
use DBD::SQLite;

use constant dbpath => "/var/lib/sqlite/db/oui.sqlite";
my ($dbh, $query);

$dbh = DBI->connect("dbi:SQLite:dbname=".dbpath,"","");
die "Can't connect to database: $!\n" unless defined $dbh;

my $ua = LWP::UserAgent->new;
$ua->agent('OUI::Update/0.1b; isquared.nl ');
my $ieee_req = HTTP::Request->new(GET => 'http://standards.ieee.org/regauth/oui/oui.txt');
my $ieee_ouitxt = $ua->request($ieee_req);

if ( ! $ieee_ouitxt->is_success ) {
  die $ieee_ouitxt->status_line;
};

my ($oui, $mfg);
for (split /^/, $ieee_ouitxt->content) {
  chomp;
  if (  m/
         ([[:xdigit:]]{2})-
         ([[:xdigit:]]{2})-
         ([[:xdigit:]]{2})
         \s+\(hex\)\s+(.+)
     /x
  ) {
      $oui = "$1:$2:$3";
      $mfg = $4;

      print "OUI: $oui \n";
      print "MFG: $mfg \n";

                $query = $dbh->prepare( q{
         INSERT OR REPLACE INTO mfg (oui, mfgname)
             VALUES (?,?);
                } );
                $query->execute($oui, $mfg);
  };
}

$dbh->disconnect;

Comments