MAC addresses

Media Access Control (MAC) is a unique identifier for network interface addresses with 6-byte fields normally written in hexadecimal.

Tools such as ipconfig show the value of a MAC address with each hexadecimal byte separated by a hyphen, for example, 1a-2b-3c-4d-5f-6d.

Linux or Unix-based systems tend to separate each hexadecimal byte with :. This includes the Linux and Unix variants, VMWare, JunOS (Juniper network device operating system, based on FreeBSD), and so on, for example, 1a:2b:3c:4d:5f:6d.

Cisco IOS shows a MAC address as three two-byte pairs separated by a period (.), for example, 1c2b.3c4d.5f6d.

A regular expression can be created to simultaneously match all of these formats.

To match a single hexadecimal character, the following character class may be used:

[0-9a-f] 

To account for the first two formats, a pair of hexadecimal characters is followed by a hyphen or a colon:

[0-9a-f]{2}[-:] 

This pattern is repeated 5 times, followed by one last pair:

([0-9a-f]{2}[-:]){5}[0-9a-f]{2} 

Adding the Cisco format into the mix will make the expression a little longer:

(([0-9a-f]{2}[-:]?){2}[-:.]){2}([0-9a-f]{2}[-:]?){2} 

Another approach is to keep the formats separate and use the alternation operator to divide the two possibilities:

([0-9a-f]{2}[-:]){5}[0-9a-f]{2}|([0-9a-f]{4}.){2}[0-9a-f]{4} 

A small script can be written to test the regular expressions against some strings. In the following tests, the first pattern is expected to fail when testing against the Cisco IOS format:

$patterns = '^([0-9a-f]{2}[-:]){5}[0-9a-f]{2}$', 
            '^(([0-9a-f]{2}[-:]?){2}[-:.]){2}([0-9a-f]{2}[-:]?){2}$', 
            '^([0-9a-f]{2}[-:]){5}[0-9a-f]{2}|([0-9a-f]{4}.){2}[0-9a-f]{4}$' 
$strings = '1a-2b-3c-4d-5f-6d', 
           '1a:2b:3c:4d:5f:6d', 
           '1c2b.3c4d.5f6d' 
foreach ($pattern in $patterns) { 
    Write-Host "Testing pattern: $pattern" -ForegroundColor Cyan 
    foreach ($string in $strings) { 
        if ($string -match $pattern) { 
            Write-Host "${string}: Matches" -ForegroundColor Green 
        } else { 
            Write-Host "${string}: Failed" -ForegroundColor Red 
        } 
    } 
} 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.10.1