Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

PowerShell Convert MAC address to Link-Local IPv6 address

I received one query on one of the chat group I am part of asking if there is a way to convert MAC physical address to Link-Local IPV6 address using PowerShell, Infact I had worked on the same kind project of migrating from IPv4 to IPv6 and already written it long bank. Lets see what is the formula behind.

convertto-ipv6.ps1 microsoft powershell, convert mac address to  ipv6 address select-object mac to local link ip address.png

Note: Link-local addresses are not necessarily bound to the MAC addresses.

Here I am creating IPv6 Link-Local Address with network adapter MAC address (Network adapter can be physical or virtual), MAC addresses are 6 bytes (48-bits) in length, I will convert it to 128 bit IPv6 address, Here is the formula for the same, For example I have hexadecimal mac address here 52:15:5D:26:42:09,  It need to be add ff:fe in the middle, so it will become 52:15:5D:ff:fe:26:42:09. Take first octet 52 and use second digit 2, convert this hexadecimal bit to binary. For example hexadecimal 2 is 0010 in binary, Now to convert further, I inverted 3rd bit, it means 0010 will become 0000, which represents value 0 in hexadecimal  [Here I have json table helps to understand hexadecimal to binary and vice versa as below (x represent the original hexadecimal value, y represents what will happen when I convert it to binary)], so 52 became 50. The complete notation become 50:15:5D:ff:fe:26:42:09.  Next I will format it in the IPv6 notation 5015:5Dff:fe26:4209, In the last prepend the link-local prefix fe80::5015:5Dff:fe26:4209 and your Link Local IPv6 Address is ready. 

[
	{
		    xHex: '0',
        xBin: '0000',
        yHex: '2',
        yBin: '0010'

	},
	{
		    xHex: '1',
        xBin: '0001',
        yHex: '3',
        yBin: '0011'
	},
	{
		    xHex: '2',
        xBin: '0010',
        yHex: '0',
        yBin: '0000'
	},
	{
		    xHex: '3',
        xBin: '0011',
        yHex: '1',
        yBin: '0001'

	},
	{
		    xHex: '4',
        xBin: '0100',
        yHex: '6',
        yBin: '0110'
	},
  {
		    xHex: '5',
        xBin: '0101',
        yHex: '7',
        yBin: '0111'
	},
	{
		    xHex: '6',
        xBin: '0110',
        yHex: '4',
        yBin: '0100'
	},
	{
		    xHex: '7',
        xBin: '0111',
        yHex: '5',
        yBin: '0101'
	},
	{
		    xHex: '8',
        xBin: '1000',
        yHex: 'a',
        yBin: '1010'
	},
	{
		    xHex: '9',
        xBin: '1001',
        yHex: 'b',
        yBin: '1011'
	},
	{
		    xHex: 'a',
        xBin: '1010',
        yHex: '8',
        yBin: '1000'
	},
	{
		    xHex: 'b',
        xBin: '1011',
        yHex: '9',
        yBin: '1001'
	},
	{
		    xHex: 'c',
        xBin: '1100',
        yHex: 'e',
        yBin: '1110'
	},
	{
		    xHex: 'd',
        xBin: '1101',
        yHex: 'f',
        yBin: '1111'
	},
	{
		    xHex: 'e',
        xBin: '1110',
        yHex: 'c',
        yBin: '1100'
	},
	{
		    xHex: 'f',
        xBin: '1111',
        yHex: 'd',
        yBin: '1101'
	}
]

Download script Convertto-IPV6.ps1 here or it is also available on https://github.com/kunaludapi.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<#  
    .Synopsis  
    Convert MAC Address to IPV6.
    .Description  
    This script converts given Mac Address to IPV6.
    .Example  
    .\Convertto-IPV6.ps1 -Mac 00:00:00:00:00:00
        
    It takes provided Mac and converts to reusable IPV6 address
    
    .Notes
    NAME: Convertto-IPV6.ps1
    Version: 1.2
    AUTHOR: Kunal Udapi
    CREATIONDATE: 5 May 2017
    LASTEDIT: 14 May 2020
    KEYWORDS:Convert MAC Address to IPV6.
    .Link  
    #Check Online version: http://kunaludapi.blogspot.com (Initially written for)
    #Requires -Version 3.0  
    #>  
   ########################################################################  
   # Generated On: 5 May 2017
   # Generated By: vCloud-lab.com  
   # Tested On: last tested on Windows 10, Earlier tested on Windows 8.1
   # For any question drop an question to kunaludapi@gmail.com
   ########################################################################

   #http://www.sput.nl/internet/ipv6/ll-mac.html

[CmdletBinding()]
param(  
    [Parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)]
    [alias('MacAddress')]
    [String]$Mac = '00:15:5D:26:42:09'
) #param

$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
$rawHexTable = Get-Content $scriptPath\hexTable.json 

$hexTable = $rawHexTable | ConvertFrom-Json
$insert = "{0}:ff:fe:{1}" -f $mac.Substring(0,8), $mac.Substring(9)
$splitIPV6Node = $insert -split ':'
$rawNotation = @()
for ($i=0; $i -lt $splitIPV6Node.Count; $i++) 
{
    #$splitIPV6Node[$i]
    if ([math]::Floor($i%2) -eq 0) 
    {
        $reFormat = $splitIPV6Node[$i]
    }
    else 
    {
        $reFormat += $splitIPV6Node[$i]
        $rawNotation += $reFormat
    }
}
$IPV6Notation = $rawNotation -Join ':'
$firstNodeBin = $splitIPV6Node[0].substring(0,1)
$secondNodeBin = $splitIPV6Node[0].substring(1,1)
$secondProcessedHex = $hexTable | Where-Object {$_.xHex -eq $secondNodeBin} | Select-Object -ExpandProperty yHex
$completeIPV6 = "fe80::{0}{1}{2}" -f $firstNodeBin, $secondProcessedHex,$IPV6Notation.Substring(2)
$completeIPV6

Just for more information there are few online MAC address to IPv6 Link local converters available online you can check the same on http://www.sput.nl/internet/ipv6/ll-mac.html.

$mac = '00:15:5D:26:42:09'
Invoke-WebRequest -Uri "https://ben.akrin.com/ipv6_mac_address_to_link_local_converter/?mode=api&mac=$mac" | Select-Object Content


$ipv6 = 'fe80::215:5dff:fe26:4209'
Invoke-WebRequest -Uri "https://ben.akrin.com/ipv6_link_local_to_mac_address_converter/?mode=api&ipv6=$ipv6" | Select-Object Content

At the moment of writing I found few online APIs, Which you can try yourself.

Powershell convert physical network adapter mac address to IPV6 link local address Invoke-WebRequest fe80 ff fe 128 bit 48 bit invoke-webrequest convert uri select-object.png

Useful Articles
Powershell one liner: Create multiple user accounts
Active Directory Powershell: Create bulk users from CSV file
Active Directory Powershell: Aduser A value for the attribute was not in the acceptable range of values
Powershell Active Directory: ADGroup Managedby - Checkbox Manager can update membership list

Go Back

Comment

Blog Search

Page Views

11358679

Follow me on Blogarama