I have heeded the advice of my wise elders and attempted to turn this script into a module.
package OUILookup;
require Exporter;
use LWP::Simple;
use strict;
use warnings;
our @ISA = qw(Exporter);
our @EXPORT = qw(get_oui_owner set_oui_location);
our $VERSION = 1.00;
my $oui_address = "http://standards.ieee.org/regauth/oui/oui.txt";
my $oui_location = "oui.txt";
sub get_oui_owner {
my ($mac) = @_;
my $oui = validate_input($mac);
my $oui_entry;
my $foundit = 0;
if ($oui eq "invalid address format") {
return $oui;
}
else {
if (update_ouidb()) {
open(OUI, $oui_location) or die "Unable to locate $oui_loc
+ation: $!\n";
while (<OUI>) {
chomp;
if (/$oui/) {
$oui_entry = $_;
$foundit = 1;
last;
}
}
close(OUI);
if (!$foundit) {
return "OUI not found";
}
else {
$oui_entry =~ s/\w{2}-\w{2}-\w{2}|^\w{6}//g;
$oui_entry =~ s/\(hex\)|\(base 16\)//g;
$oui_entry =~ s/^\s+//g;
return $oui_entry;
}
}
}
}
sub update_ouidb {
my $errcode = mirror("http://standards.ieee.org/regauth/oui/oui.tx
+t", $oui_location);
if (!($errcode eq "200" || "304")) {
die "problem updating OUI database: $!\n";
}
else {
return 1;
}
}
sub validate_input {
my ($input) = @_;
my @bytes;
my $oui;
if ($input =~ /\w{2}-\w{2}-\w{2}-\w{2}-\w{2}-\w{2}/) {
@bytes = split '-', $input;
$oui = join '-', $bytes[0], $bytes[1], $bytes[2];
return $oui;
}
else {
return "invalid address format";
}
}
sub set_oui_location {
my ($new_value) = @_;
$oui_location = $new_value;
}
1;
=head1 NAME
OUILookup
=head1 SYNOPSIS
Find the owner of the IEEE Organizationally Unique Identifier (OUI)
for a particular MAC address.
OUILookup uses the IEEE database from <http://standards.ieee.org/regau
+th/oui/oui.txt>.
It will create a local copy of that database if one does not already e
+xist, and update
a the local copy if it does exist.
By default, the local copy will be stored as oui.txt in the same direc
+tory as the script.
This location may be set by set_oui_location($new_location).
=head1 USAGE
use OUILookup;
$mac = "00-10-5A-1B-C3-30";
$owner = get_oui_owner($mac);
=head1 REQUIRES
LWP::Simple
=head1 TESTED
Perl 5.6.0 on Windows NT (Service Pack 6)
=head1 AUTHOR
Micah Valine <mvaline@buffnet.net>
=cut
Updated: Thanks to Allowing user to change module variables, I have added set_oui_location to allow the user to specify where the oui.txt file is mirrored.