Hey, Scripting Guy! How can I display the IP addresses associated with a given MAC address?
— NK
Hey, NK. Before we answer your question we should take a moment to answer a related question. People often ask us, “If I have a MAC address can I write a script that will tell me which computer on my network is using that address?” Unfortunately, the answer to that question is this: no, you can’t write a script that somehow searches the network looking for a particular MAC address. The best you can do is bind to each individual computer and check to see if the MAC address can be found on that machine. For example, you could write a script that retrieves all your computer names from Active Directory and then methodically connects to each of those computers, checking to see if that MAC address can be found. But that’s something we won’t talk about today.
Note. However, you can find sample code for retrieving computer names from Active Directory, from a text file, and from other sources in the Remote/Multiple Computer Templates section of the Script Repository. |
But if all you want to do is check for a MAC address (and return the associated IP addresses) on a given computer, well, that’s easy:
On Error Resume NextstrComputer = “.” Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set colItems = objWMIService.ExecQuery _ (“Select * from Win32_NetworkAdapterConfiguration Where MACAddress = ’99:99:99:AA:99:A9′”)
For Each objItem in colItems For Each strIPAddress in objItem.IPAddress Wscript.Echo “IP Address: ” & strIPAddress Next Next
As you can see, there’s nothing fancy here at all. The script begins by connecting to the WMI service on the local computer (although we could just as easily connect to the WMI service on a remote computer). We then use this line of code to retrieve a collection of all the network adapters that have the MAC address 99:99:99:AA:99:A9:
Set colItems = objWMIService.ExecQuery _ (“Select * from Win32_NetworkAdapterConfiguration Where MACAddress = ’99:99:99:AA:99:A9′”)
At this point all we have to do is walk through the collection and echo back the IP addresses associated with that network adapter. The key here – and a mistake many people make – is that we’re echoing the IP addresses (plural) for each network adapter. It’s entirely possible for a single network adapter to have multiple IP addresses; because of that, the IPAddress property stores values as an array. That means we need to set up a second For Each loop to walk through that collection:
For Each strIPAddress in objItem.IPAddress Wscript.Echo “IP Address: ” & strIPAddress Next
Bear in mind that we need to do this even if there’s one (or even if there aren’t any) IP address associated with the adapter.
What if there aren’t any network adapters with that particular MAC address? Well, in this case you just won’t get back any data. However, we could add a tiny bit of code that takes action based on the value of the collection’s Count property: if this value is 0, that means there are no items in the collection, and thus no network adapters with that MAC address. Here’s a modified script that reports back the appropriate message if no such adapter can be found:
On Error Resume NextstrComputer = “.” Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set colItems = objWMIService.ExecQuery _ (“Select * from Win32_NetworkAdapterConfiguration Where MACAddress = ’99:99:99:AA:99:A9′”)
If colItems.Count = 0 Then Wscript.Echo “There are no adapters with that MAC address.” Wscript.Quit End If
For Each objItem in colItems For Each strIPAddress in objItem.IPAddress Wscript.Echo “IP Address: ” & strIPAddress Next Next
And, just for the heck of it, here’s a bonus script, one that simply reports back the names and MAC addresses for all the network adapters on a computer. Admittedly this is a script you could easily write yourself, but, hey, now you don’t have to:
strComputer = “.” Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)Set colItems = objWMIService.ExecQuery _ (“Select * from Win32_NetworkAdapter”)
For Each objItem in colItems Wscript.Echo objItem.Name, objItem.MACAddress Next
0 comments