{"id":69603,"date":"2005-06-14T11:01:00","date_gmt":"2005-06-14T11:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/06\/14\/how-can-i-associate-a-network-connection-with-an-ip-address\/"},"modified":"2005-06-14T11:01:00","modified_gmt":"2005-06-14T11:01:00","slug":"how-can-i-associate-a-network-connection-with-an-ip-address","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-associate-a-network-connection-with-an-ip-address\/","title":{"rendered":"How Can I Associate a Network Connection with an IP 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 determine the local area connection that\u2019s associated with a particular IP address?<BR><BR>&#8212; SH<\/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, SH. Believe it or not, this is a fairly complicated procedure, and for two reasons. To begin with, we have to use two separate WMI classes &#8211; <B>Win32_NetworkAdapter<\/B> and <B>Win32_NetworkAdapterConfiguration<\/B> &#8211; in order to retrieve the information we need. That\u2019s because network connection information is stored in one class (Win32_NetworkAdapter) and IP addresses are stored in another (Win32_NetworkAdapterConfiguration). On top of that, Windows considers all sorts of things &#8211; wired connections, wireless connections, VPN connections, FireWire ports, you name it &#8211; to be network connections. This means we have quite a bit of data to wade through before we can link a specific network connection to a specific ID.<\/P>\n<P>Oh, yeah: and IP addresses are stored as arrays. This isn\u2019t a major problem, but it does represent one more hurdle we have to overcome.<\/P>\n<P>OK, now that you all feel sufficiently sorry for us (\u201cOh, those poor Scripting Guys; they have to work so <I>hard<\/I> to try and answer our questions!\u201d) let\u2019s show you a script that reports back the network connection associated with the IP address 192.59.244.247:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nstrTargetAddress = &#8220;192.59.244.247&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colItems = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True&#8221;)<\/p>\n<p>For Each objItem in colItems\n    arrIPAddresses = objItem.IPAddress\n    For Each strAddress in arrIPAddresses\n        If strAddress = strTargetAddress Then\n            strMACAddress = objItem.MacAddress\n        End If\n    Next\nNext<\/p>\n<p>Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_NetworkAdapter Where MACAddress = &#8216;&#8221; &amp; strMACAddress &amp; &#8220;&#8216;&#8221;)<\/p>\n<p>For Each objItem in colItems\n    If Not IsNull(objItem.NetConnectionID) Then\n        Wscript.Echo objItem.NetConnectionID\n    End If\nNext\n<\/PRE>\n<P>After assigning values to the variables strComputer and strTargetAddress (which represent the computer we\u2019re working with and the target IP address, respectively) we bind to the WMI service and then use the <B>ExecQuery<\/B> method to select all instances of the Win32_NetworkAdapterConfiguration class where the <B>IPEnabled<\/B> property is equal to True. This returns only TCP\/IP-enabled network adapters, and weeds out things like FireWire connections. <\/P>\n<P>We then set up a For Each loop to cycle through the returned collection. Inside that For Each loop we assign the value of the <B>IPAddress<\/B> property of our first network adapter to an array variable named arrIPAddresses. (Don\u2019t be fooled by the named IPAddress; this property can contain more than one IP address, which is why we need to use an array to get at the values.) We then set up a second For Each loop to loop through the array of IP addresses. For each address we check to see if the value matches our target IP address (192.59.244.247). If it does, we assign the value of the <B>MACAddress<\/B> property to a variable named strMACAddress. We continue this process until we\u2019ve checked all the IP addresses for all the network adapters.<\/P>\n<P>When we exit the loop we will know the MAC address of the network adapter associated with our target IP address. Believe it or not, that\u2019s the key to unlocking this mystery. Why? Because the MACAddress property is found in both of our WMI classes. As soon as we know the MAC address associated with the IP address 192.59.244.247 all we have to do find this same MAC address somewhere in the Win32_NetworkAdapter class. When we do that, we can then determine the name of the network connection associated with that IP address. <\/P>\n<P>And that\u2019s exactly what we do with this second WMI query, one that retrieves all instances of the Win32_NetworkAdapter class where the MACAddress is equal to the MAC address associated with our IP address:<\/P><PRE class=\"codeSample\">Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_NetworkAdapter Where MACAddress = &#8216;&#8221; &amp; strMACAddress &amp; &#8220;&#8216;&#8221;)\n<\/PRE>\n<P>Relax; we\u2019re almost done here. We now loop through the returned collection, eliminating any instances where the <B>NetConnectionID<\/B> property is null. Eventually we\u2019ll get back a message like this one:<\/P><IMG border=\"0\" alt=\"Network connection\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/netconnect.jpg\" width=\"170\" height=\"107\"> \n<P><BR>OK, sure, it doesn\u2019t <I>look<\/I> like much. But this just happens to be the name of the network connection associated with the IP address 192.59.244.247. And that ought to be worth <I>something<\/I>, right?<\/P>\n<P>Incidentally, you can find some additional information about associating network connections and network adapters in the whitepaper <A href=\"http:\/\/null\/technet\/scriptcenter\/topics\/networking\/02_atnc_basic.mspx\"><B>Automating TCP\/IP Networking on Clients<\/B><\/A>. Click the link and then search for <I>Associating the Network Connections Name with MAC and IP Addresses Using Two Classes<\/I>.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine the local area connection that\u2019s associated with a particular IP address?&#8212; SH Hey, SH. Believe it or not, this is a fairly complicated procedure, and for two reasons. To begin with, we have to use two separate WMI classes &#8211; Win32_NetworkAdapter and Win32_NetworkAdapterConfiguration &#8211; in order to retrieve [&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-69603","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 determine the local area connection that\u2019s associated with a particular IP address?&#8212; SH Hey, SH. Believe it or not, this is a fairly complicated procedure, and for two reasons. To begin with, we have to use two separate WMI classes &#8211; Win32_NetworkAdapter and Win32_NetworkAdapterConfiguration &#8211; in order to retrieve [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69603","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=69603"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69603\/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=69603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}