How to get list of ethernet mac addresses when not connected?

Hello all,

In linux there is a command to get a list of Ethernet ports, either all or only wireless or only wired. I am looking for the equivalent for Windows. Does anyone know how to do this? The built in Xojo commands only get those that are connected. I need them even if not connected.

Thanks,
Tim

This is what I wound up doing. Not very elegant, but it appears to work!

Public Function getFirstEthernetMAC_Windows() as String
  Dim tMAC As String
  #If TargetWindows Then
    Dim theShell As New Shell
    Dim theVolumesData(-1) As String
    theShell.Execute "ipconfig /all"    //MAC Address -
    theVolumesData = Split(ReplaceLineEndings(theShell.ReadAll, EndOfLine), EndOfLine)
    For i As Integer = 0 To theVolumesData.Ubound -1
      If theVolumesData(i) = "Ethernet adapter Ethernet:" Then
        tMAC = theVolumesData(i+5)
        Dim w As Integer = InStr(0,tmac,Chr(45) )
        Dim L As Integer = Len(tMAC)
        w = w -2
        tMAC = Mid(tMAC, w, L-w +1)
        tMAC = ReplaceAll(tMAC, Chr(45), Chr(58) )
        Exit For
      End If
    Next i
  #EndIf
  
  Return tMAC
End Function

Nice, just be aware that it’ll only work in an English locale.

Hi Julian.

Hmmm… yea, that would be a problem wouldn’t it. Maybe even UK English might be different…

Gonna have to work on it more. I thought for sure there would be a WIndows API to call. I honestly did not spend much time looking into that. Gonna have to though.

Thanks for the compliment and revealing the weakness!
Tim

maybe here is some help:
https://www.maketecheasier.com/view-network-adapter-details-in-windows/

https://msdn.microsoft.com/en-us/library/windows/desktop/aa365915(v=vs.85).aspx

Its a bit of a complicated structure, but thats the way to do it in code without using an external app.

But you’ll want:

getmac.exe

That’s existed since Winddows XP.

See https://technet.microsoft.com/en-gb/library/bb490913.aspx for more info including csv output :slight_smile:

Don’t forget though, that the first might not be the “main” connection.

Have fun :slight_smile:

[quote=341445:@]https://msdn.microsoft.com/en-us/library/windows/desktop/aa365915(v=vs.85).aspx

Its a bit of a complicated structure, but thats the way to do it in code without using an external app.

But you’ll want:

getmac.exe

That’s existed since Winddows XP.

See https://technet.microsoft.com/en-gb/library/bb490913.aspx for more info including csv output :slight_smile:

Don’t forget though, that the first might not be the “main” connection.

Have fun :)[/quote]

I used GetMac as you said Julian when I ran into this tonight. I needed to get the wifi mac address from IoT boards without connecting the board to the network. This is what I did, if anyone comes up with a way to prevent the empty array elements on split let me know.

[code]Public Function GetMacs() as String()
Dim s as String
Dim strMacInfo() As String

Dim sh As New Shell
sh.Mode = 0
sh.Execute(“GetMac /nh /fo csv”)

s = sh.Result
s = ReplaceAll(s, “-”, “:”)
s = ReplaceLineEndings(s, “”)
strMacInfo = Split(s, chr(34))

for i as integer = strMacInfo.Ubound DownTo 0
if InStr(strMacInfo(i), “,”) > 0 then strMacInfo.Remove(i)
if InStr(strMacInfo(i), "") > 0 then strMacInfo.Remove(i)
if InStr(strMacInfo(i), “/”) > 0 then strMacInfo.Remove(i)
if InStr(strMacInfo(i), “not”) > 0 then strMacInfo.Remove(i)
if InStr(strMacInfo(i), “disconnected”) > 0 then strMacInfo.Remove(i)
next i

’ Would like to remove this step somehow…
for i as integer = strMacInfo.Ubound DownTo 0
if strMacInfo(i) = “” then strMacInfo.Remove(i)
next i

Return strMacInfo
End Function[/code]

Hows this?

[code]Public Function GetMacs() as String()
Dim strMacInfo() As String

Dim sh As New Shell
sh.Mode = 0
sh.Execute(“GetMac /nh /fo table”)

Dim s As String
s = sh.Result

Dim re As New RegEx
Dim match As regexmatch

re.SearchPattern = “^([0-9A-Fa-f]{2}[-]){5}([0-9A-Fa-f]{2})”

match = re.Search(s)
Do
If match <> Nil Then
strMacInfo.Append(match.SubExpressionString(0))
End If
match = re.Search()
Loop Until match Is Nil

Return strMacInfo
End Function
[/code]

[quote=386382:@]Hows this?

[code]Public Function GetMacs() as String()
Dim strMacInfo() As String

Dim sh As New Shell
sh.Mode = 0
sh.Execute(“GetMac /nh /fo table”)

Dim s As String
s = sh.Result

Dim re As New RegEx
Dim match As regexmatch

re.SearchPattern = “^([0-9A-Fa-f]{2}[-]){5}([0-9A-Fa-f]{2})”

match = re.Search(s)
Do
If match <> Nil Then
strMacInfo.Append(match.SubExpressionString(0))
End If
match = re.Search()
Loop Until match Is Nil

Return strMacInfo
End Function
[/code][/quote]

Oh that’s perfect. Regular expression for a mac address. :slight_smile:

I just lifted it from here, you can put the : back in the [:-] if you want to make it multi-purpose and use it elsewhere as some places represent MACs with : not - I took it out just in case.

Yeah I was just adding the : chars back (for my purposes) and reading that exact stackoverflow page. Nice find thanks.

If you use MBS Xojo Network Plugin, you can try the NetworkInterfaceMBS class.