Powershell: Check MAC Address in range

By YellowOnline on Friday 18 September 2015 14:23 - Comments (6)
Category: Powershell, Views: 3.706

This tiny script will check if a MAC address falls within a valid range. I hope you'll never need it, but I did (thank you VMware for restricting MAC Address ranges across ESX versions...).

It's a monster (isn't line 12 lovely?), I know. Suggestions to do this more elegantly - regex comes to mind - are welcome of course.

PowerShell:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Function Validate-MACRange ($MACaddressToValidate)
    {
    $ValidRangeFrom = '00:50:56:00:00:00'
    $ValidRangeTo   = '00:50:56:3F:FF:FF'
    $a = $MACaddressToValidate.Replace("-",":").Split(":")
    $b = $ValidRangeFrom.Replace("-",":").Split(":")
    $c = $ValidRangeTo.Replace("-",":").Split(":")
    $i = 0
    $Valid = $True
    Do
        {
        If ($([int]"0x$($a[$i])" -GE [int]"0x$($b[$i])") -And ([int]"0x$($a[$i])" -LE [int]"0x$($c[$i])") -EQ $False){$Valid = $False}
        $i++
        }
    Until ($i -EQ 6)
    Return $Valid;
    }


Het monstertje leeft...

Edit
Here's another approach that is faster (thanks dugo & even more erwin80):

PowerShell:
1
2
3
4
5
6
7
8
9
10
Function Validate-MACRange2 ($MACaddressToValidate) 
    {
    $ValidRangeFrom = '00:50:56:00:00:00'
    $ValidRangeTo   = '00:50:56:3F:FF:FF'
    $a = [long] ("0x"+$MACaddressToValidate.Replace(":","").Replace("-",""))
    $b = [long] ("0x"+$ValidRangeFrom.Replace(":","").Replace("-",""))
    $c = [long] ("0x"+$ValidRangeTo.Replace(":","").Replace("-",""))
    $Valid = $a -GE $b -AND $a -LE $c
    Return $Valid;
    }



The first method took 4456 ticks, the second 2807. Quite a difference.

Volgende: Migrating the Windows DHCP role between languages, versions and architectures 10-'15 Migrating the Windows DHCP role between languages, versions and architectures
Volgende: Koken! Frangipanetaart 09-'15 Koken! Frangipanetaart

Comments


By Tweakers user HyperBart, Friday 18 September 2015 16:04

...het leeft diep in mij

By Tweakers user monstertje.xyz, Friday 18 September 2015 16:05

You rang?

...... I'm alive!!

[Comment edited on Friday 18 September 2015 16:06]



By Tweakers user duqo, Friday 18 September 2015 22:01

Why convert and compare every octet separately? Isn't it easier to just convert the entire mac address to a UInt64 and compare those? :)

By Tweakers user erwin80, Friday 18 September 2015 23:30

Function Validate-MACRange ($MACaddressToValidate) {
$a = [long] ("0x"+$MACaddressToValidate.Replace(":",""));
$b = [long] ("0x"+$ValidRangeFrom.Replace(":",""));
$c = [long] ("0x"+$ValidRangeTo.Replace(":",""));
return ($a < $b && $b < $c);
}

Something like that, but i'm drunk and it's hard to concentrate :)
Also, there is a PhysicalAddress class in c# which might be of use.

By Tweakers user YellowOnline, Saturday 19 September 2015 10:09

duqo wrote on Friday 18 September 2015 @ 22:01:
Why convert and compare every octet separately? Isn't it easier to just convert the entire mac address to a UInt64 and compare those? :)
erwin80 wrote on Friday 18 September 2015 @ 23:30:
Function Validate-MACRange ($MACaddressToValidate) {
$a = [long] ("0x"+$MACaddressToValidate.Replace(":",""));
$b = [long] ("0x"+$ValidRangeFrom.Replace(":",""));
$c = [long] ("0x"+$ValidRangeTo.Replace(":",""));
return ($a < $b && $b < $c);
}

Something like that, but i'm drunk and it's hard to concentrate :)
Also, there is a PhysicalAddress class in c# which might be of use.
Absolutely. I was afraid of the mathematics involved I think. See blogpost for more.

In order to comment on this post you need to be logged in. Use this link to log in when you are already a registered user. If you don't have an account you can create one here.