Created
February 26, 2014 09:57
-
-
Save hh-lohmann/9226867 to your computer and use it in GitHub Desktop.
Function to get the MAC address of the current computer for a given internet (IP) connection, i.e. that of the network adapter with which an IP address out of a given range of IP addresses is connected.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' | |
' function getMacAddressForIpRange | |
' | |
' Returns the MAC address of the current computer for a given internet (IP) connection, i.e. that of the network adapter with which an IP address out of a given range of IP addresses is connected. The restriction to an IP address range avoids to wrongly retrieve virtual adapters like VirtualBox. Note that technically only the first matching MAC address is returned, assuming that only one connection to the same net exists per machine. | |
' | |
' !! May not work in virtual machines. | |
' | |
' @license GPL 2 (http://www.gnu.org/licenses/gpl.html) | |
' @author hh.lohmann <hh.lohmann@yahoo.de> | |
' @version 2014-02-26 10:15 | |
' @param strIpRange IP address range for a given connection | |
' -> stated as the beginning of IP addresses in the same range, e.g. "192.168." | |
' @return string: MAC address | |
' | |
' PROGRAM CONCEPT | |
' Use WMI to query the Win32_NetworkAdapterConfiguration class for IPEnabled adapters (see (2)), in these adapters find that one whose IP address is in the given IP address range (checked by string comparison). | |
' | |
' REFERENCES | |
' (1) WMI: http://en.wikipedia.org/wiki/Windows_Management_Instrumentation | |
' (2) Win32_NetworkAdapterConfiguration class: http://msdn.microsoft.com/en-us/library/aa394217%28v=vs.85%29.aspx | |
' | |
Function getMacAddressForIpRange ( strIpRange ) | |
Set WMI = GetObject("winmgmts:\\.\root\cimv2") | |
Set NetWorkAdapters = WMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=true") | |
For Each Adapter In NetWorkAdapters | |
For i = LBound ( Adapter.IPAddress ) To UBound ( Adapter.IPAddress ) | |
If ( Left( Adapter.IPAddress( i ), Len( strIpRange ) ) = strIpRange ) Then | |
getMacAddressForIpRange = Adapter.MACAddress | |
Exit Function | |
End If | |
Next | |
Next | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment