Get the IP address of WIFI interface

Tim Golden mail at timgolden.me.uk
Sun May 15 15:38:40 EDT 2011


On 15/05/2011 20:23, Jun Hu wrote:
> Thanks for the tip, it is really helpful!
> however the class of Win32_NetworkAdapterConfiguration doesn't include
> the interface type (you can NOT tell if it is a wifi interface), so I
> change the code a bit like following:
>
> import wmi
>
> wlan_int_id=None
> for nic in wmi.WMI().Win32_NetworkAdapter():
>      if nic.NetConnectionID == "Wireless Network Connection":
>          wlan_int_id=nic.Index
>          break
>
> if wlan_int_id<>None:
>      for nic in wmi.WMI ().Win32_NetworkAdapterConfiguration (IPEnabled=1):
>          if nic.Index==wlan_int_id:
>              print nic.IPAddress[0]
> else:
>      print "WLAN interface NOT Found"

Glad it was useful; you can get a little bit prettier:

<code>
import wmi

c = wmi.WMI ()
for nic in c.Win32_NetworkAdapter (
   NetConnectionID="Wireless Network Connection"
):
   for config in nic.associators (
     wmi_result_class="Win32_NetworkAdapterConfiguration"
   ):
     print config.Caption, "=>", " / ".join (config.IPAddress)
   break
else:
   print "No Wireless NIC found"

</code>


TJG



More information about the Python-list mailing list