{"id":68113,"date":"2006-01-24T14:57:00","date_gmt":"2006-01-24T14:57:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/01\/24\/how-can-i-display-the-ip-addresses-associated-with-a-given-mac-address\/"},"modified":"2006-01-24T14:57:00","modified_gmt":"2006-01-24T14:57:00","slug":"how-can-i-display-the-ip-addresses-associated-with-a-given-mac-address","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-display-the-ip-addresses-associated-with-a-given-mac-address\/","title":{"rendered":"How Can I Display the IP Addresses Associated with a Given MAC Address?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I display the IP addresses associated with a given MAC address?<BR><BR>&#8212; NK<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, NK. Before we answer your question we should take a moment to answer a related question. People often ask us, \u201cIf I have a MAC address can I write a script that will tell me which computer on my network is using that address?\u201d Unfortunately, the answer to <I>that<\/I> question is this: no, you can\u2019t 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\u2019s something we won\u2019t talk about today.<\/P>\n<TABLE id=\"E3C\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. However, you <I>can<\/I> find sample code for retrieving computer names from Active Directory, from a text file, and from other sources in the <A href=\"http:\/\/null\/technet\/scriptcenter\/scripts\/templates\/default.mspx\"><B>Remote\/Multiple Computer Templates<\/B><\/A> section of the Script Repository.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>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\u2019s easy:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_NetworkAdapterConfiguration Where MACAddress = &#8217;99:99:99:AA:99:A9&#8242;&#8221;)<\/p>\n<p>For Each objItem in colItems\n    For Each strIPAddress in objItem.IPAddress\n        Wscript.Echo &#8220;IP Address: &#8221; &amp; strIPAddress\n    Next\nNext\n<\/PRE>\n<P>As you can see, there\u2019s 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:<\/P><PRE class=\"codeSample\">Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_NetworkAdapterConfiguration Where MACAddress = &#8217;99:99:99:AA:99:A9&#8242;&#8221;)\n<\/PRE>\n<P>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 &#8211; and a mistake many people make &#8211; is that we\u2019re echoing the IP <I>addresses<\/I> (plural) for each network adapter. It\u2019s entirely possible for a single network adapter to have multiple IP addresses; because of that, the <B>IPAddress<\/B> property stores values as an array. That means we need to set up a second For Each loop to walk through <I>that<\/I> collection:<\/P><PRE class=\"codeSample\">For Each strIPAddress in objItem.IPAddress\n    Wscript.Echo &#8220;IP Address: &#8221; &amp; strIPAddress\nNext\n<\/PRE>\n<P>Bear in mind that we need to do this even if there\u2019s one (or even if there aren\u2019t any) IP address associated with the adapter.<\/P>\n<P>What if there aren\u2019t <I>any<\/I> network adapters with that particular MAC address? Well, in this case you just won\u2019t get back any data. However, we could add a tiny bit of code that takes action based on the value of the collection\u2019s <B>Count<\/B> 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\u2019s a modified script that reports back the appropriate message if no such adapter can be found:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_NetworkAdapterConfiguration Where MACAddress = &#8217;99:99:99:AA:99:A9&#8242;&#8221;)<\/p>\n<p>If colItems.Count = 0 Then\n    Wscript.Echo &#8220;There are no adapters with that MAC address.&#8221;\n    Wscript.Quit\nEnd If<\/p>\n<p>For Each objItem in colItems\n    For Each strIPAddress in objItem.IPAddress\n        Wscript.Echo &#8220;IP Address: &#8221; &amp; strIPAddress\n    Next\nNext\n<\/PRE>\n<P>And, just for the heck of it, here\u2019s 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\u2019t have to:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_NetworkAdapter&#8221;)<\/p>\n<p>For Each objItem in colItems\n    Wscript.Echo objItem.Name, objItem.MACAddress\nNext<\/PRE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I display the IP addresses associated with a given MAC address?&#8212; NK Hey, NK. Before we answer your question we should take a moment to answer a related question. People often ask us, \u201cIf I have a MAC address can I write a script that will tell me which computer [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[36,37,3,5],"class_list":["post-68113","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-client-side-management","tag-networking","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I display the IP addresses associated with a given MAC address?&#8212; NK Hey, NK. Before we answer your question we should take a moment to answer a related question. People often ask us, \u201cIf I have a MAC address can I write a script that will tell me which computer [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=68113"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68113\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=68113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}