2014-12-27 20:58:59 -08:00
|
|
|
package DDG::Goodie::MacAddress;
|
|
|
|
# ABSTRACT: Vendor information lookup for MAC addresses
|
|
|
|
|
2015-02-22 12:09:29 -08:00
|
|
|
use strict;
|
2014-12-27 20:58:59 -08:00
|
|
|
use DDG::Goodie;
|
|
|
|
|
|
|
|
zci answer_type => "mac_address";
|
|
|
|
zci is_cached => 1;
|
|
|
|
|
|
|
|
triggers startend => "mac address", "ethernet address";
|
|
|
|
|
2014-12-28 21:43:13 -08:00
|
|
|
my %oui_db = map { chomp; my (@f) = split(/\\n/, $_, 2); ($f[0] => $f[1]); } share("oui_database.txt")->slurp;
|
2014-12-27 20:58:59 -08:00
|
|
|
|
|
|
|
sub fmt_mac {
|
|
|
|
my $mac = shift;
|
|
|
|
$mac = lc($mac);
|
|
|
|
$mac =~ s/..\K(?=.)/:/g;
|
|
|
|
$mac;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle remainder => sub {
|
|
|
|
return unless $_;
|
|
|
|
return unless $_ =~ m|^[-.:/ 0-9a-f]+$|i;
|
|
|
|
$_ =~ s/[^0-9a-fA-F]//g;
|
|
|
|
return unless (length($_) == 12 || length($_) == 16);
|
|
|
|
|
|
|
|
my ($oui) = uc(substr($_, 0, 6));
|
|
|
|
my ($info) = $oui_db{$oui};
|
|
|
|
return unless $info;
|
|
|
|
my (@vendor) = split(/\\n/, $info, 2);
|
|
|
|
|
|
|
|
my ($name, $addr) = map { html_enc($_); } @vendor;
|
|
|
|
$addr = "No associated address" unless defined $addr;
|
2015-02-01 11:21:32 -08:00
|
|
|
|
2015-02-01 11:53:18 -08:00
|
|
|
# If the info is all capitals, then try to add in some best guesses for
|
|
|
|
# capitalization to make it more readable.
|
|
|
|
#
|
|
|
|
# Decide whether to do this replacement per-line, since there are often
|
|
|
|
# errant unformatted lines amongst formatted ones.
|
|
|
|
my (@lines) = split(/\\n/, $info);
|
|
|
|
foreach my $line (@lines) {
|
|
|
|
if ($line !~ m/[a-z]/) {
|
|
|
|
$line =~ s/(\w+)/ucfirst(lc($1))/eg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($result) = join("", map { "<p class=\"macaddress\">$_</p>"; } @lines);
|
2015-02-01 12:07:24 -08:00
|
|
|
$result =~ s/class="macaddress"/class="macaddress title"/;
|
2014-12-27 20:58:59 -08:00
|
|
|
|
2016-05-16 05:29:46 -07:00
|
|
|
my $oui_formatted = fmt_mac($oui);
|
|
|
|
my $text_answer = "The OUI, $oui_formatted, for this NIC is assigned to $name";
|
2016-05-16 05:27:16 -07:00
|
|
|
return $text_answer, structured_answer => {
|
|
|
|
data => {
|
|
|
|
answer => $result,
|
2016-05-16 05:29:46 -07:00
|
|
|
input => $oui_formatted
|
2016-05-16 05:27:16 -07:00
|
|
|
},
|
|
|
|
templates => {
|
|
|
|
content => 'DDG.mac_address.content'
|
|
|
|
}
|
|
|
|
};
|
2014-12-27 20:58:59 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
1;
|