matching only OUI portion of MAC address

E

erik

I am using this regex pattern:

$addr =~s/.*([\w \d]{2}:[\w \d]{2}:[\w \d]{2}).*/$1/;

I am trying to match the first "nibbles" of the MAC address. So for MAC
00:00:0c:07:ac:65, I would only want 00:00:0c.

Oddly enough, my regex pattern match the last 3 nibbles, e.g. 07:ac:65.

My datastream is a "arp -an" on a solaris box. Format is like:

root $ arp -an

Net to Media Table: IPv4
Device IP Address Mask Flags Phys Addr
------ -------------------- --------------- ----- ---------------
eri0 xx.xx.xx.xx 255.255.255.255 00:00:0c:07:ac:65
eri0 xx.xx.xx.xx 255.255.255.255 08:00:20:ad:58:ec

I have tried many different regex patterns and after 2 hours I am
getting grey hairs. Can anyone help?
 
J

James Taylor

I am using this regex pattern:

$addr =~s/.*([\w \d]{2}:[\w \d]{2}:[\w \d]{2}).*/$1/;

The first .* gobbles the whole line (because quantifiers are
greedy by default) and then the regex matching engine has to
backtrack from the end of the line until it is able to fit
the rest of the regex to the last three "nibbles" (using
your non-standard definition of nibbles).

You could simply put a question mark after the star to make in
non-greedy (like this .*? ) but I think there's a better way.
root $ arp -an

Net to Media Table: IPv4
Device IP Address Mask Flags Phys Addr
------ -------------------- --------------- ----- ---------------
eri0 xx.xx.xx.xx 255.255.255.255 00:00:0c:07:ac:65
eri0 xx.xx.xx.xx 255.255.255.255 08:00:20:ad:58:ec

Well, the MAC addresses are distinctive in containing colons without
spaces either side, and I presume that anything appearing in the Flags
column would be separated from the MAC address by at least one space.
Therefore, just match the first three "nibbles" like this:

my $arp_output = `arp -an`;
my @vendors = $arp_output =~ / ([\da-f]{2}:[\da-f]{2}:[\da-f]{2})/g;
 
J

John W. Krahn

Jim said:
erik said:
I am using this regex pattern:

$addr =~s/.*([\w \d]{2}:[\w \d]{2}:[\w \d]{2}).*/$1/;

I am trying to match the first "nibbles" of the MAC address. So for MAC
00:00:0c:07:ac:65, I would only want 00:00:0c.

Oddly enough, my regex pattern match the last 3 nibbles, e.g. 07:ac:65.

My datastream is a "arp -an" on a solaris box. Format is like:

root $ arp -an

Net to Media Table: IPv4
Device IP Address Mask Flags Phys Addr
------ -------------------- --------------- ----- ---------------
eri0 xx.xx.xx.xx 255.255.255.255 00:00:0c:07:ac:65
eri0 xx.xx.xx.xx 255.255.255.255 08:00:20:ad:58:ec

Have you thought of using substr($_,48,8)? Not every problem needs to
be solved with a regex.

You could also use split (untested):

$mac = split;

split() in scalar or void context will put the results into @_ and issue a
warning. You probably meant:

my $mac = (split)[-1];
@mac = split(/:/,$mac);
$addr = join(':',@mac[0..2]);


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top