Hello,

I'm new to perl and to this site (it has been an HUGE help) and I'm having a problem I am hoping someone can assist me with solving. Basically I am trying to prevent a lengthy manual process of searching the OUI of a routers ARP cache to find the Company. We have tools but no access so I'm stuck with finding a faster way. The problem I'm having is I can match the first iteration of the ARP cache hash but afterwards it stops matching altogether and just iterates the rest with blank results. Below is the code I have so far, please be kind as I am still learning, a small arp cache file and the resulting output :) Thanks in advance for any and all assistance.

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 @data = (); # Declared Array to be created +with the input file for searching the MAC address. 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 an array. while (<INPUT>) { # This is the ARP + Cache file created from a copy/paste of a routers push @data, "$_"; # show arp comm +and. } close(INPUT); for (@data) { # This section iterates over the ARP cache array and + pulls the IP and MAC address out and stores it into a hash if ($_ =~ m/([\d]+\.[\d]+\.[\d]+\.[\d]+).*([\w]{4})\.([\w]{4})\.([\w +]{4})/i) { # Key is MAC my $temp = $2.$3.$4; + # Value is IP $hash{$temp} = $1; } } while ( my ( $key, $value ) = each %hash ) { my $match = substr $key, 0, 6; say "$hash{$key} mapped to $key for company $oui{$match}!"; }

this is the input file tech.txt (IP and MAC addresses changed to protect my job) :D

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

This is the reulting output

10.10.10.2 mapped to 00132004acde for company Intel Corporate!

10.10.10.3 mapped to 08004ec894ac for company !

10.10.10.1 mapped to 000b462d2846 for company !

As you can see I can only get the first iteration to match and can only seem to get it using subtr, afterwards it just stops. I can iterate through the hash just fine and print it out, seems from the examples I've found it should work.

I have tried various other ways but thats the furthest I have been able to get so far as most of the time I dont get any matches on the Company name.

Also, I wasn't sure how to get the oui.txt up in the post for testing as it's a pretty big file. Thanks again. :)

In reply to Regex matching part of one hash key to another by KevinNC

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.