简体   繁体   中英

Perl REGEX on mac address

I am using the command "show mac-address-table dynamic" on a cisco switch via perl script. The output for the same as is follows:

Legend: * - primary entry
    age - seconds since last seen
    n/a - not available

vlan   mac address     type    learn     age              ports
------+----------------+--------+-----+----------+--------------------------
*   14  782b.cb87.b085   dynamic  Yes          5   Gi4/39
*  400  0017.c59a.23aa   dynamic  Yes         15   Gi3/37
*  400  0017.c59a.23aa   dynamic  Yes          5   Gi1/27
*  400  0017.c50f.704d   dynamic  Yes          5   Gi13/19
*  400  0006.8b05.a915   dynamic  Yes         10   Gi5/29
*  400  c0ea.e414.2f29   dynamic  Yes         10   Gi3/37
*  400  0017.c53e.166d   dynamic  Yes          5   Gi1/12

I am writing a script such that it takes out only the mac address having initial first 4 figures such as 0017.c5 or 0006b or c0ea.e4. Also, it should display the number of times they are repeated. I tried by writing the below code:

for my $line (@ver) {
    if (my ($mac_addr) = $line =~ /((?:[0-9a-f]{4}\.){2}[0-9a-f]{4})/) {
        $mac_addr = $1;
        if ($mac_addr =~ m/^(0017.c5)[0-9a-f]{2}.[0-9a-f]{4}/ or $mac_addr =~ m/^(c0ea.e5)[0-9a-f]{2}.[0-9a-f]{4}/ or $mac_addr =~ m/^(0006.b)[0-9a-f]{3}.[0-9a-f]{4}/){
            push (@sonic_macaddr, $mac_addr);
            for $it (@sonic_macaddr){
                $uniq{$it}++;
            }
        }
    }
}
print (Dumper(\%uniq));

I am not getting the required results. Please can somebody guide me where I am going wrong? Thanks.

This match will always fail:

$mac_addr =~ m/^(0017.c5)[.]/

That looks for your desired prefix, followed by a period. None of your mac address will have a period at the 8th character.

Perhaps the following reworking of your script will help you:

use strict;
use warnings;

my @ver = <DATA>;

my @mac_prefixes = qw(0017.c5 c0ea.e5 0006.b);
my $mac_prefixes = '(?:' . join('|', map quotemeta, @mac_prefixes) . ')';

my @sonic_macaddr;
my %uniq;

for my $line (@ver) {
    if (my ($mac_addr) = $line =~ /((?:[0-9a-f]{4}\.){2}[0-9a-f]{4})/) {
        $mac_addr = $1;
        if ($mac_addr =~ m/^($mac_prefixes)/){
            my $prefix = $1;
            push (@sonic_macaddr, $mac_addr);
            $uniq{$prefix}++;
        }
    }
}

use Data::Dump;
dd \%uniq;
dd \@sonic_macaddr;

__DATA__
Legend: * - primary entry
    age - seconds since last seen
    n/a - not available

vlan   mac address     type    learn     age              ports
------+----------------+--------+-----+----------+--------------------------
*   14  782b.cb87.b085   dynamic  Yes          5   Gi4/39
*  400  0017.c59a.23aa   dynamic  Yes         15   Gi3/37
*  400  0017.c59a.23aa   dynamic  Yes          5   Gi1/27
*  400  0017.c50f.704d   dynamic  Yes          5   Gi13/19
*  400  0006.8b05.a915   dynamic  Yes         10   Gi5/29
*  400  c0ea.e414.2f29   dynamic  Yes         10   Gi3/37
*  400  0017.c53e.166d   dynamic  Yes          5   Gi1/12

First you have problem with your regex. Just remove the [.] from the regex.

Secondly, you are pushing the result into the @sonic_macaddr array and then increasing the $uniq for every match. It is giving you wrong count. So remove that portion.

Here is how your if block looks like after the modification.

if ($mac_addr =~ m/^(0017.c5)/ or $mac_addr =~ m/^(c0ea.e5)/ or $mac_addr =~ m/^(0006.b)/){
    $uniq{$mac_addr}++;
}

Output for your sample Data:

$VAR1 = {
      '0017.c53e.166d' => 1,
      '0017.c59a.23aa' => 2,
      '0017.c50f.704d' => 1
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM