http://qs321.pair.com?node_id=97093

The first three octets of an ethernet MAC address comprise the Organizational Unique Identifier (OUI) granted to ethernet card manufacturers by the IEEE. Occasionally, it is useful to be able to find the manufacturer of a particular card, so I wrote a script to do so.

If you call it like ouilookup.pl <OUI>, it searches a local copy of the list of registered OUIs available from the IEEE at http://standards.ieee.org/regauth/oui/oui.txt. If the -i option is used, it actually searches the file on the IEEE website.

This is not a perfect way to find the manufacturer because according to the IEEE, not all companies assigned an OUI have elected to make their OUI public. In addition, because subcontracting, the holder of the OUI may not be the actual manufacturer.

#/usr/bin/perl -w # # ouilookup.pl - print out the manufacturer to whom a MAC ID is regist +ered. # by Micah Valine <mvaline@buffnet.net> # # Under normal operation, the program searches oui.txt, the location o +f # which is specified in the variable $oui_location, below. oui.txt may # by downloaded from <http://standards.ieee.org/regauth/oui/oui.txt>. # # If the -i option is used, the program will examine the file directly # from the IEEE website. It's significantly slower, but one can be sur +e # you are getting up to date information. # use warnings; use strict; use IO::Socket; # program settings my $oui_location = "x:\\bin\\oui.txt"; # global variables my $oui; my $foundit = 0; my $input_type; my $output; my $optioni = 0; my $server = "standards.ieee.org"; my $port = "80"; my $location = "/regauth/oui/oui.txt"; my $server_sock; # usage: print usage information and die sub usage() { print STDERR << "ENDUSAGE"; usage: maclookup.pl [-i] OUI -i searches the IEEE website rather than a local file ENDUSAGE exit 1; } # malformed: tell the user his input doesn't look right sub malformed { print STDERR << "ENDMALFORMED"; I'm sorry, but $ARGV[0] doesn't look like a MAC ID to me. You may ente +r MAC IDs in two formats: hexadecimal, which looks like (XX-XX-XX), or base 16, which looks like (XXXXXX). ENDMALFORMED exit 1; } # notfound: tell the user that we didn't find his input sub notfound { print STDERR << "ENDNOTFOUND"; Unfortunately, I couldn't find a listing for $ARGV[0]. You may wish to download an updated copy of oui.txt from IEEE at http://standards.ieee.org/regauth/oui/oui.txt. ENDNOTFOUND exit 1; } #notfoundi: tell the user we didn't find his input on the internet sub notfoundi { print STDERR << "ENDNOTFOUNDI"; Unfortunately, I couldn't find a listing for $ARGV[1]. I was looking at the file on the IEEE website, so I'm pretty sure the number is an unregistered one. ENDNOTFOUNDI exit 1; } # program entry point usage() unless ($ARGV[0]); if ($ARGV[0] eq "-i") { $optioni = 1; if ($ARGV[1] =~ /\w{2}-\w{2}-\w{2}/) { $input_type = "hex"; $oui = $ARGV[1]; } elsif ($ARGV[1] =~ /\w{6}/) { $input_type = "base16"; $oui = $ARGV[1]; } else { malformed(); } } elsif ($ARGV[0] =~ /\w{2}-\w{2}-\w{2}/) { $input_type = "hex"; $oui = $ARGV[0]; } elsif ($ARGV[0] =~ /\w{6}/) { $input_type = "base16"; $oui = $ARGV[0]; } else { malformed(); } # perform the search if (!$optioni) { open(OUI, $oui_location) or die "Sorry, I can't find $oui_location +: $!\n"; while (<OUI>) { chomp; if (/$oui/) { $output = $_; $foundit = 1; last; } } close(OUI); if (!$foundit) { notfound(); } else { $output =~ s/\w{2}-\w{2}-\w{2}|^\w{6}//g; $output =~ s/\(hex\)|\(base 16\)//g; $output =~ s/^\s+//g; print "$output\n"; } } else { $server_sock = IO::Socket::INET->new(Proto=>"tcp", PeerAddr=>$serv +er, PeerPort=>"$port", Reuse=>1); $server_sock->autoflush(1); # set buffering off print $server_sock "GET $location HTTP/1.1\nHost: $server:$port\n\ +n"; while (<$server_sock>) { chomp; if (/$oui/) { $output = $_; $foundit = 1; last; } } close($server_sock); if (!$foundit) { notfoundi(); } else { $output =~ s/\w{2}-\w{2}-\w{2}|^\w{6}//g; $output =~ s/\(hex\)|\(base 16\)//g; $output =~ s/^\s+//g; print "$output\n"; } } exit;

Replies are listed 'Best First'.
Re: Find Ethernet Card Manufacturer
by DrZaius (Monk) on Jul 16, 2001 at 22:45 UTC
    I find myself saying this a lot, but look into Getopt::Long and Pod::Usage for command line scripts. It cleans up a lot of logic for you and using pod is nicer for manpages than comment blocks :)

    Also, when a global is also a constant, use constant instead. This makes it easy to maintain your code as you know what is constant and what the script will probably modify.

    I see you aren't using LWP. What's wrong with you? You can use LWP::Simple if you don't feel like messing around with user agents and such. Not to mention you'll be able to handle far more errors :)

    Also, consider changing this into a module. It seems more useful in that state than a command line script. You could put the mac address database into the __DATA__ section of the module or specify and arg to check a webpage.

    Just some thoughts.

      Thank you for the good advice; I had never before heard of use constant, Pod::Usage, or LWP. They will be very useful to me... especially LWP. I have a lot of scripts that grab stuff from the web and I always go through the rigamarole I did in this script.

      I'm going to try creating a module as you suggested. I've never tried that before, so it will be a good learning experience.

Re: Find Ethernet Card Manufacturer
by mvaline (Friar) on Jul 18, 2001 at 22:39 UTC
    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.