Getting MAC Address

How to retrieve and format wifi MAC address in MicroPython on ESP32?

I am also a little suspicious of the bytes retrieved from wlan_sta.config('mac'). I would have expected something that looked more like b'\xaa\xbb\xcc\x11\x22\x33' instead of b'0\xae\xa4z\xa7$'. The z and the $ seem very out of place for something that should be hexadecimal and it seems too short for what should be six pairs of digits.

You're not getting back a hexadecimal string, you're getting a byte string. So if the MAC address contains the value 7A, then the byte string will contain z (which has ASCII value 122 (hex 7A)).



Am I using the correct method to get the MAC address?

You are!

If it is correct, how can I format it as six pairs of hex digits?

If you want to print the MAC address as a hex string, you can use the
ubinascii.hexlify method:

>>> import ubinascii
>>> import network
>>> wlan_sta = network.WLAN(network.STA_IF)
>>> wlan_sta.active(True)
>>> wlan_mac = wlan_sta.config('mac')
>>> print(ubinascii.hexlify(wlan_mac).decode())
30aea47aa724

Or maybe:

>>> print(ubinascii.hexlify(wlan_mac).decode().upper())
30AEA47AA724

How to find the MAC Address of Active Directory Computer Objects?

You will need to loop over the computers and get the MAC address individually inside the loop:

# Get-ADComputer returns these properties by default: 
# DistinguishedName, GroupCategory, GroupScope, Name, ObjectClass, ObjectGUID, SamAccountName, SID

$props = 'OperatingSystem', 'OperatingSystemVersion', 'OperatingSystemServicePack', 'IPv4Address'
$result = Get-ADComputer -Filter "operatingsystem -like '*Windows server*' -and enabled -eq 'true'" -Properties $props |
Sort-Object OperatingSystem | ForEach-Object {
$mac = if ((Test-Connection -ComputerName $_.Name -Count 1 -Quiet)) {
# these alternative methods could return an array of MAC addresses

# get the MAC address using the IPv4Address of the computer
(Get-CimInstance -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled='True'" -ComputerName $_.IPv4Address).MACAddress
# or use
# Invoke-Command -ComputerName $_.IPv4Address -ScriptBlock { (Get-NetAdapter | Where-Object {$_.Status -eq 'Up'}).MacAddress }
# or
# (& getmac.exe /s $_.IPv4Address /FO csv | ConvertFrom-Csv | Where-Object { $_.'Transport Name' -notmatch 'disconnected' }).'Physical Address'
}
else { $mac = 'Off-Line' }

# return the properties you want as object
$_ | Select-Object Name, OperatingSystem, OperatingSystemVersion, OperatingSystemServicePack, IPv4Address,
@{Name = 'MacAddress'; Expression = {@($mac)[0]}}
}

# output on screen
$result | Format-Table -AutoSize -Wrap

# or output to CSV file
$result | Export-Csv -Path 'X:\Wherever\ADComputers.csv' -NoTypeInformation -UseCulture

Get lowest mac address

You can use Sort-Object to sort the output from Get-NetAdapter based on the .MacAddress property and then get the lowest Mac Address based on that sort.

Get-NetAdapter "Intel(R) I210*" | Sort-Object MacAddress

For the lowest you would simply pipe it to Select-Object -First 1.

I was previously using Sort-Object { [PhysicalAddress] $_.MacAddress } however this is not even needed as Lee_Dailey pointed out.



Related Topics



Leave a reply



Submit