Parser/ Importer for the IEEE OUI list


In a distant past I created this 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.
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.

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.
The script is also available for download at http://isquared.nl/src/oui_import.pl.


	#!/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;