#!/usr/bin/perl -Tw

# This file is in the public domain.
# Stuart Henderson, December 2011

sub readin($)
{
	my ($file) = @_;

	if (open (IN, $file)) {
		while (<IN>) {
			chomp;
			if (/\t/) {
				my ($mac, $vendor) = split('\t');
				$oui{lc($mac)} = $vendor;
			}
		}
		close (IN);
	}
}

readin('/usr/local/share/arp-scan/ieee-oui.txt');
readin('/usr/local/share/arp-scan/ieee-iab.txt');
readin('/usr/local/share/arp-scan/mac-vendor.txt');

die("couldn't read any MAC list") unless %oui;

while(<>) {
	chomp;
	my $line = $_;
	# ([0-9a-f]{2}:){5}[0-9a-f]{2}	00:11:22:33:44:55
	# [0-9a-f]{6}-[0-9a-f]{6}	001122-334455 (HP Procurve)
	if ( /(([0-9a-f]{2}:){5}[0-9a-f]{2}|[0-9a-f]{6}-[0-9a-f]{6})/i ) {
		my $m = $1;
		my $vendor = 'UNKNOWN';
		my $n = lc($1);
		$n =~ s/[-:]//g;
		while ($n ne '') {
			$n =~ s/..$//;
			if ($oui{$n}) {
				$vendor = $oui{$n};
				last;
			}
		}
		$line =~ s,$m,$m ($vendor),;
	}
	print "$line\n";
}

