#/usr/bin/perl -w # # ouilookup.pl - print out the manufacturer to whom a MAC ID is registered. # by Micah Valine # # Under normal operation, the program searches oui.txt, the location of # which is specified in the variable $oui_location, below. oui.txt may # by downloaded from . # # 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 sure # 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 enter 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 () { 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=>$server, 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;