def collect_networks(): # collect networks and useful metadata def is_commotion_in_bssList(bssList): for bss in bssList: if commotion_BSSID_re.match(bss.bssid): return True return False ifaces = collect_interfaces() nets = [] nets_dict = {} for iface in ifaces: # SSID<one-many>BSSID # WW.gWNBL gives BSSIDs with SSID each nets_bss = WindowsWifi.getWirelessNetworkBssList(iface) # WW.gWANL gives SSIDs with BSSID count and sec info nets_avail = WindowsWifi.getWirelessAvailableNetworkList(iface) # need SSID and sec info to construct profile # need SSID, profile, and preferred BSSIDs for WW.connect() for net_avail in nets_avail: net = {"interface": iface, "auth": net_avail.auth, "cipher": net_avail.cipher, "bss_list": [], "commotion": False} for bss in nets_bss: if bss.ssid == net_avail.ssid: net["bss_list"].append(bss) if not net["commotion"]: # one commotion BSSID marks the SSID as commotion net["commotion"] = bool( commotion_BSSID_re.match(bss.bssid)) nets_dict[(iface.netsh_name, bss.ssid, bss.bssid)] = { "interface": iface, "ssid": bss.ssid, "bssid": bss.bssid, "dot11_bss_type": bss.bss_type, "bss_type": dot11_to_wlan[bss.bss_type], "auth": net_avail.auth, "cipher": net_avail.cipher, "quality": bss.link_quality } nets.append(net) return nets, ifaces, nets_dict
def get_wlan_current_connection(PyWiWi_iface): ''' Returns connection attributes if connected, None if not. ''' iface_state = get_wlan_interface_state(PyWiWi_iface)[1] print "current iface state", iface_state if iface_state == "wlan_interface_state_connected": cnx, CNX = WindowsWifi.queryInterface(PyWiWi_iface, 'current_connection') else: cnx, CNX = None, None return cnx, CNX
def wlan_connect(iface, spec): # PyWiWi.WindowsWifi.connect() only works reliably in profile mode. # So we use that. We need it because the netsh wlan connect doesn't # allow BSSID specification. cnxp = {"connectionMode": "wlan_connection_mode_profile", "profile": spec["profile_name"], "ssid": spec["ssid"], "bssidList": [spec["bssid"]], "bssType": spec["dot11_bss_type"], "flags": 0} result = WindowsWifi.connect(iface, cnxp) print "connecting to", spec["profile_name"], "; result:", result
def collect_interfaces(): ifaces = WindowsWifi.getWirelessInterfaces() wmi_ifaces = wmi.WMI().Win32_NetworkAdapter() ifaces_by_guid = {} for wmi_iface in wmi_ifaces: ifaces_by_guid[wmi_iface.GUID] = wmi_iface for iface in ifaces: wmi_iface = ifaces_by_guid[iface.guid_string] wmi_iface_conf = wmi.WMI().Win32_NetworkAdapterConfiguration( InterfaceIndex=wmi_iface.InterfaceIndex)[0] # functions needed to restore initial state iface.EnableDHCP = wmi_iface_conf.EnableDHCP iface.EnableStatic = wmi_iface_conf.EnableStatic iface.SetGateways = wmi_iface_conf.SetGateways # preserve initial state iface.initial_connection = get_current_connection(iface) iface.netsh_name = wmi_iface.NetConnectionID iface.MAC = wmi_iface.MACAddress iface.IPs = wmi_iface_conf.IPAddress iface.subnet_masks = wmi_iface_conf.IPSubnet iface.gateways = wmi_iface_conf.DefaultIPGateway iface.DHCP_enabled = wmi_iface_conf.DHCPEnabled return ifaces
def get_wlan_profile_xml(PyWiWi_iface, profile_name): return WindowsWifi.getWirelessProfileXML(PyWiWi_iface, profile_name)
def get_wlan_interface_state(PyWiWi_iface): s, S = WindowsWifi.queryInterface(PyWiWi_iface, 'interface_state') return s, S