2

We are using the following method to get the MAC addresses.

Imports System.Net.NetworkInformation
Dim nic As NetworkInterface = Nothing
Dim max() As String
Dim i As Integer
Dim nics() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
For Each nic In nics
    max(i) = nic.GetPhysicalAddress().ToString
    i = i + 1
Next

Using the above code we could successfully get all the MAC addresses including the MAC address of a dongle[Plug & Play Device]. But what we wanted is the MAC address of only internal Devices.

How can we get the MAC address of only internal Devices those can be called permanent MAC addresses?

SHREE
  • 53
  • 1
  • 8

4 Answers4

0

I assume the endpoints in your LAN gets a private IP (as a DHCP response) and the dongle (an external USB GSM modem) gets a global IP address.

The standard ranges for private IP addresses:

  • class A network 10.0.0.0 - 10.255.255.255
  • class B network 172.16.0.0 - 172.31.255.255
  • class C network 192.168.0.0 - 192.168.255.255

You can utilize a regular expression to determine for each interface if it's current ip address is private or not.

you may assist this regular expression (source):

(^10\.)|
(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|
(^192\.168\.)

for example, this is a basic regex match of the above in VB.net:

Imports System.Net.NetworkInformation
Imports System.Text.RegularExpressions

Module VBModule

    Sub Main()
        Dim nic As NetworkInterface = Nothing
        Dim max() As String
        Dim nics() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
        For Each nic In nics

            Dim ip = nic.GetIPProperties().UnicastAddresses(0).Address.ToString

            Dim regex As Regex = New Regex("(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)")
            Dim match As Match = regex.Match(ip)

            If match.Success Then
                Console.WriteLine(ip)
                Console.WriteLine(nic.GetPhysicalAddress().ToString)     
            End If

        Next

    End Sub

End Module

See it in action http://goo.gl/i9N6T9:

Community
  • 1
  • 1
Jossef Harush Kadouri
  • 32,361
  • 10
  • 130
  • 129
  • None of the MAC are matching with the IP(If match.Success). Infact the IP of each thing is something like this --> "fe80::e997:bc09:37a4:c67c%12" – SHREE Mar 25 '15 at 08:14
  • This does not answer the question. Remember that the answer must apply to **other use cases**, and not only the use case of this question. – AStopher Mar 25 '15 at 08:25
  • while this approach will work everytime it requires your NICs to be configured for static networks ... which is very cumbersome IMHO – specializt Mar 25 '15 at 10:29
0

The code below returns MAC addresses for the most common durable interfaces (LAN or WLAN). This usually gets the job done. You can add different interface types to the comparison array.

Public Function GetDurableMACAddresses() As IEnumerable(Of String)
    Return From nic In NetworkInterface.GetAllNetworkInterfaces()
           Where ({NetworkInterfaceType.Ethernet, NetworkInterfaceType.Wireless80211}).Contains(nic.NetworkInterfaceType)
           Select macaddress = nic.GetPhysicalAddress().ToString()
End Function
Craig Johnson
  • 744
  • 4
  • 8
-1

The below function can be used to get the internal MAC address:

Private Function getMacAddress() As String
    Try
        Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
        Dim adapter As NetworkInterface
        Dim myMac As String = String.Empty

        For Each adapter In adapters
            Select Case adapter.NetworkInterfaceType
                'Exclude Tunnels, Loopbacks and PPP
                Case NetworkInterfaceType.Tunnel, NetworkInterfaceType.Loopback, NetworkInterfaceType.Ppp
                Case Else
                    If Not adapter.GetPhysicalAddress.ToString = String.Empty And Not adapter.GetPhysicalAddress.ToString = "00000000000000E0" Then
                        myMac = adapter.GetPhysicalAddress.ToString
                        Exit For
                    End If

            End Select
        Next adapter

        Return myMac
    Catch ex As Exception
        Return String.Empty
    End Try
End Function

It uses the Network Information library, which can be imported using Imports System.Net.NetworkInformation. The above code also excludes VPNs, tunnels, bridges, etc.

To test this I made a sample project which can be downloaded here, and gave the correct MAC address output as:

The system's Media Access Control (MAC) address is 142D27B97FD6

This is the correct MAC address as tested by viewing the properties of my wireless adapter:

enter image description here

Community
  • 1
  • 1
AStopher
  • 4,207
  • 11
  • 50
  • 75
  • How sure are you that the index(1) of MAC is the internal device's MAC? and What about the address at index(0)? – SHREE Mar 25 '15 at 08:39
  • @SHREE It's the adapter number. I'm sure that it is the internal devices' MAC address because of the screenshot I provided. Updated the code to fix the slight error in the MAC address. – AStopher Mar 25 '15 at 08:44
  • @cybermoneky is there any chance that the system does not have a MAC? and usually what will be at the 0th Index? – SHREE Mar 25 '15 at 08:49
  • @SHREE The system will always have a MAC address if it has a network adapter. In the case that it does not the `Try Catch` statement will catch the error and return an empty string. You could always have some more error handling such as a `MsgBox` to notify the user that they do not have a network adapter installed. – AStopher Mar 25 '15 at 08:51
  • @cybermonkey When I plug the dongle and run the program I get the MAC - "E89A8FA35AD0" and when I remove it I get the MAC - ""D0DF9AC58E03". However MAC of dongle is "001E101F0000" All the time the external devices MAC will have 0th position? – SHREE Mar 25 '15 at 08:57
  • @cybermonkey what if there are more external dongles connected? Index(1) will be an external device right? So this coding might go wrong!? – SHREE Mar 25 '15 at 08:59
  • @SHREE I don't think it's possible to check whether the adapter is plugged in via USB, I'm looking into it. – AStopher Mar 25 '15 at 09:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/73744/discussion-between-yekuomreqy-and-shree). – AStopher Mar 25 '15 at 10:01
  • @SHREE Please give me the requested information in the chat. – AStopher Mar 25 '15 at 10:22
  • @specializt It's annoying because I saw them going into the chat, then they left. The adapter types have 'Wireless' and 'Ethernet', but also 'Wired'; want to see if an external adapter is recognised differently. – AStopher Mar 25 '15 at 10:31
  • it is not recognized differently, of course. That'd be really problematic since there would be no common device-class in that case. Under linux you'd be able to extrapolate the bustype via its nodename - this is also possible with WINAPI but its unreliable. Under windows, the only reliable way of retrieving such information is via advanced hardware APIs like SetupAPI. – specializt Mar 25 '15 at 10:33
-2

Without additional information about the NIC itself it is not possible to identitfy "internal" ones - but : if you build a CLR (C++) - library which calls native functions to retrieve necessary information like "on which bus is the NIC located" you may be able to assemble grouping information about the NICs.

Hint : it is easily possible to use CLR-libraries within your VB.NET - application ... simply add the project, import its namespace and you're good to go : https://msdn.microsoft.com/en-us/library/ms235638%28v=vs.90%29.aspx

At first, you will need to query all NICs via SetupDiGetClassDevs with the parameter GUID_DEVCLASS_NET, then enumerate that list with SetupDiEnumDeviceInfo, the property DEVPKEY_Device_BusTypeGuid is what you're interested in, you can retrieve it via SetupDiGetDeviceProperty - after all your devices are enumerated you are now able to group them into bus-types, at least one of them is the USB bustype.

Additional filtering may be done via all of the other properties listed here, here and here.

Well, thats quite the expensive solution but it will actually enable you to filter out the "internal" NICs, good luck - feel free to ask any questions.

edit : Apparently there is also a GUID for USB devices, so if you simply retrieve the property DEVPKEY_DeviceInterface_ClassGuid and compare it to GUID_DEVINTERFACE_USB_DEVICE you might be able to filter out USB-NICs on the fly, without the need for additional grouping ... this looks very promising

specializt
  • 1,913
  • 15
  • 26