Thanks for the tips. I am a very beginning perl user, I picked up the Llama book a month ago and have been with it off and on since. This is not my primary job function, I'm more of a NOC person but there was some free time and was given the go ahead to try and pick up some coding again to help reduce some manual processes, so most of the stuff is adaptations from the books examples and my, VERY, limited knowledge. It has been 18 years since I have written any code in school as an elective class. I'm hoping to use this more and more and become more effecient and these tips help out a lot. I have cleaned up the code based on the suggestions and have reposted here. Most of the time I use the explicits to help learn what is going on and make it clear to my n00b eyes :).
use 5.010; use strict; use warnings; # filehandle section open STDOUT, ">ouiresults.txt"; # Output file after program is ru +n. open STDERR, ">errlog.txt"; # Error Log. my $f1 = 'oui.txt'; # Filehandle of the OUI list from + the IETF used to create a hash of oui and company. my %hash = (); # Declared Hash to be created w +ith the input file to store the IP to MAC mappings. my %oui = (); # Delcared Hash containing the O +UI to Company mapping. open (LIST1, $f1) || die "File not found\n"; # Takes the oui +file and creates a hash while (<LIST1>) { # Key is OUI chomp; # Value is Company if (/\x20{5}\(base 16\)\t{2}/) { $oui{$`} = $'; } } close LIST1; open(INPUT, $ARGV[0]) || die "Cannot do eet! $!"; # Takes the inp +ut file name on the command line and builds a hash. while (<INPUT>) { # This is the ARP + Cache file created from a copy/paste of a Cisco routers arp cache if ($_ =~ m/(\d+\.\d+\.\d+\.\d+).*(\w{4})\.(\w{4})\.(\w{4})/) { + # Key is MAC my $temp = $2.$3.$4; + # Value is IP $hash{$temp} = $1; } } close(INPUT); while ( my ( $key, $value ) = each %hash ) { my $match = substr $key, 0, 6; say "$hash{$key} mapped to $key for company $oui{$match}!"; }
OUTPUT
10.147.23.11 mapped to 001320048fef for company Intel Corporate!
10.147.23.4 mapped to 08004ec802b4 for company !
10.147.23.1 mapped to 000b462d5869 for company !
INPUT
Internet 10.10.10.1 - 000b.462d.2846 ARPA FastEthernet0
Internet 10.10.10.2 1 0013.2004.acde ARPA FastEthernet0
Internet 10.10.10.3 7 0800.4ec8.94ac ARPA FastEthernet0
Thanks again for everyone's comments, I'm looking forward to more, I'm hooked!In reply to Re^2: Regex matching part of one hash key to another
by KevinNC
in thread Regex matching part of one hash key to another
by KevinNC
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |