def test_normalized_frequency(self): cell = Cell.from_string(FREQUENCY_5G) self.assertEqual(cell.frequency_norm, '5Ghz') cell = Cell.from_string(IWLIST_SCAN_WPA2) self.assertEqual(cell.frequency_norm, '2.4Ghz') cell = Cell.from_string(FREQUENCY_UNSUPPORTED) self.assertIsNone(cell.frequency_norm)
def normalize(cell_block): # The cell blocks come in with every line except the first indented at # least 20 spaces. This removes the first 20 spaces off of those lines. lines = textwrap.dedent(' ' * 20 + cell_block).splitlines() cell = Cell() while lines: line = lines.pop(0) if line.startswith('Quality'): for re_name, quality_re in quality_re_dict.items(): match_result = quality_re.search(line) if match_result is not None: cell.quality, signal = match_result.groups() if re_name == 'relative': actual, total = map(int, signal.split('/')) cell.signal = db2dbm(int((actual / total) * 100)) else: cell.signal = int(signal) break elif line.startswith('Bit Rates'): values = split_on_colon(line)[1].split('; ') # consume next line of bit rates, because they are split on # different lines, sometimes... while lines[0].startswith(' ' * 10): values += lines.pop(0).strip().split('; ') cell.bitrates.extend(values) elif ':' in line: key, value = split_on_colon(line) key = normalize_key(key) if key == 'ie': if 'Unknown' in value: continue # consume remaining block values = [value] while lines and lines[0].startswith(' ' * 4): values.append(lines.pop(0).strip()) if 'WPA2' in value: cell.encryption_type = 'wpa2' elif 'WPA' in value: cell.encryption_type = 'wpa' if key == 'frequency': frequency, channel = frequency_re.search(value).groups() cell.frequency = frequency cell.channel = int(channel) elif key in normalize_value: setattr(cell, key, normalize_value[key](value)) # It seems that encryption types other than WEP need to specify their # existence. if cell.encrypted and not hasattr(cell, 'encryption_type'): cell.encryption_type = 'wep' return cell
def test_unencrypted(self): cell = Cell() cell.ssid = 'SSID' cell.encrypted = False scheme = Scheme.for_cell('wlan0', 'test', cell) self.assertEqual(scheme.options, { 'wireless-essid': 'SSID', 'wireless-channel': 'auto', })
def test_all_calls_check_output_with_good_args(self): args = ['/sbin/iwlist', 'interface', 'scan'] kwargs = {'stderr': subprocess.STDOUT} with patch.object(subprocess, 'check_output', return_value=expected_output): Cell.all('interface') subprocess.check_output.assert_called_with(args, **kwargs) args.insert(0, 'sudo') Cell.all('interface', sudo=True) subprocess.check_output.assert_called_with(args, **kwargs)
def test_wep(self): cell = Cell() cell.ssid = 'SSID' cell.encrypted = True cell.encryption_type = 'wep' scheme = Scheme.for_cell('wlan0', 'test', cell, 'passkey') self.assertEqual(scheme.options, { 'wireless-essid': 'SSID', 'wireless-key': 'passkey', })
def test_wpa(self): cell = Cell() cell.ssid = 'SSID' cell.encrypted = True cell.encryption_type = 'wpa' scheme = Scheme.for_cell('wlan0', 'test', cell, 'passkey') self.assertEqual(scheme.options, { 'wpa-ssid': 'SSID', 'wpa-psk': 'ea1548d4e8850c8d94c5ef9ed6fe483981b64c1436952cb1bf80c08a68cdc763', 'wireless-channel': 'auto', })
def test_wpa(self): cell = Cell() cell.ssid = 'SSID' cell.encrypted = True cell.encryption_type = 'wpa' scheme = Scheme.for_cell('wlan0', 'test', cell, 'passkey') self.assertEqual( scheme.options, { 'wpa-ssid': 'SSID', 'wpa-psk': 'ea1548d4e8850c8d94c5ef9ed6fe483981b64c1436952cb1bf80c08a68cdc763', 'wireless-channel': 'auto', })
def getNetworkList(self): if self.oldInterfaceState is None: self.oldInterfaceState = iNetwork.getAdapterAttribute( self.iface, "up") if self.oldInterfaceState is False: if iNetwork.getAdapterAttribute(self.iface, "up") is False: iNetwork.setAdapterAttribute(self.iface, "up", True) Console().ePopen( ["/sbin/ifconfig", "/sbin/ifconfig", self.iface, "up"]) driver = iNetwork.detectWlanModule(self.iface) if driver == "brcm-wl": Console().ePopen(["/usr/bin/wl", "/usr/bin/wl", "up"]) try: scanResults = list(Cell.all(self.iface, 5)) print("[Wlan] Scan results = '%s'." % scanResults) except Exception: scanResults = None print("[Wlan] No wireless networks could be found.") aps = {} if scanResults: for i in range(len(scanResults)): bssid = scanResults[i].ssid aps[bssid] = { "active": True, "bssid": scanResults[i].ssid, "essid": scanResults[i].ssid, "channel": scanResults[i].channel, "encrypted": scanResults[i].encrypted, "encryption_type": scanResults[i].encryption_type if scanResults[i].encrypted else "none", "iface": self.iface, "maxrate": scanResults[i].bitrates, "mode": scanResults[i].mode, "quality": scanResults[i].quality, "signal": scanResults[i].signal, "frequency": scanResults[i].frequency, "frequency_norm": scanResults[i].frequency_norm, "address": scanResults[i].address, "noise": scanResults[i].noise, "pairwise_ciphers": scanResults[i].pairwise_ciphers, "authentication_suites": scanResults[i].authentication_suites, } return aps
def test_bssid(self): # This seems like a useless test, yet if bssid is refactored and not # in sync with address attribute, this will alert us. cell = Cell.from_string(ALTERNATIVE_OUTPUT) self.assertEqual(cell.address, cell.bssid) cell.bssid = 'AC:22:05:25:3B:6A' self.assertEqual('AC:22:05:25:3B:6A', cell.address)
def getNetworkList(self): if self.oldInterfaceState is None: self.oldInterfaceState = iNetwork.getAdapterAttribute( self.iface, "up") if self.oldInterfaceState is False: if iNetwork.getAdapterAttribute(self.iface, "up") is False: iNetwork.setAdapterAttribute(self.iface, "up", True) system("ifconfig " + self.iface + " up") if existBcmWifi(self.iface): eConsoleAppContainer().execute("wl up") aps = {} try: scanresults = list(Cell.all(self.iface, 5)) print("[Wlan.py] scanresults1 = %s" % scanresults) except: scanresults = None print("[Wlan.py] No wireless networks could be found") if scanresults is not None: for i in range(len(scanresults)): bssid = scanresults[i].ssid aps[bssid] = { 'active': True, 'bssid': scanresults[i].ssid, 'essid': scanresults[i].ssid, 'channel': scanresults[i].channel, 'encrypted': scanresults[i].encrypted, 'encryption_type': scanresults[i].encryption_type if scanresults[i].encrypted else "n/a", 'iface': self.iface, 'maxrate': scanresults[i].bitrates, 'mode': scanresults[i].mode, 'quality': scanresults[i].quality, 'signal': scanresults[i].signal, 'frequency': scanresults[i].frequency, 'frequency_norm': scanresults[i].frequency_norm, 'address': scanresults[i].address, 'noise': scanresults[i].noise, 'pairwise_ciphers': scanresults[i].pairwise_ciphers, 'authentication_suites': scanresults[i].authentication_suites, } print("[Wlan.py] apsresults1 = %s" % aps) return aps
def update(self): """Update Wifi stats using the input method. Stats is a list of dict (one dict per hotspot) :returns: list -- Stats is a list of dict (hotspot) """ # Reset stats self.reset() # Exist if we can not grab the stats if not wifi_tag: return self.stats if self.input_method == 'local': # Update stats using the standard system lib # Grab network interface stat using the PsUtil net_io_counter method try: netiocounters = psutil.net_io_counters(pernic=True) except UnicodeDecodeError: return self.stats for net in netiocounters: # Do not take hidden interface into account if self.is_hide(net): continue # Grab the stats using the Wifi Python lib try: wifi_cells = Cell.all(net) except InterfaceError: # Not a Wifi interface pass except Exception as e: # Other error logger.debug("WIFI plugin: Can not grab cellule stats ({})".format(e)) pass else: for wifi_cell in wifi_cells: hotspot = { 'key': self.get_key(), 'ssid': wifi_cell.ssid, 'signal': wifi_cell.signal, 'quality': wifi_cell.quality, 'encrypted': wifi_cell.encrypted, 'encryption_type': wifi_cell.encryption_type if wifi_cell.encrypted else None } # Add the hotspot to the list self.stats.append(hotspot) elif self.input_method == 'snmp': # Update stats using SNMP # Not implemented yet pass return self.stats
def test_no_encryption(self): cell = Cell.from_string(IWLIST_SCAN_NO_ENCRYPTION) self.assertFalse(cell.encrypted) self.assertEqual(cell.ssid, 'My Wireless Network') self.assertEqual(cell.signal, -51) self.assertEqual(cell.quality, '59/70') self.assertEqual(cell.frequency, '2.437 GHz') self.assertEqual(cell.mode, 'Master') self.assertEqual(cell.channel, 6)
def test_no_encryption(self): cell = Cell.from_string(IWLIST_SCAN_NO_ENCRYPTION) self.assertFalse(cell.encrypted) self.assertEqual('My Wireless Network', cell.ssid) self.assertEqual(-51, cell.signal) self.assertEqual('59/70', cell.quality) self.assertEqual('2.437 GHz', cell.frequency) self.assertEqual('Master', cell.mode) self.assertEqual(6, cell.channel)
def getNetworkList(self): if self.oldInterfaceState is None: self.oldInterfaceState = iNetwork.getAdapterAttribute( self.iface, "up") if self.oldInterfaceState is False: if iNetwork.getAdapterAttribute(self.iface, "up") is False: iNetwork.setAdapterAttribute(self.iface, "up", True) system("ifconfig " + self.iface + " up") driver = iNetwork.detectWlanModule(self.iface) if driver in ('brcm-wl', ): system("wl up") scanresults = list(Cell.all(self.iface)) aps = {} if scanresults is not None: for i in range(len(scanresults)): bssid = scanresults[i].ssid aps[bssid] = { 'active': True, 'bssid': scanresults[i].ssid, 'essid': scanresults[i].ssid, 'channel': scanresults[i].channel, 'encrypted': scanresults[i].encrypted, 'encryption_type': scanresults[i].encryption_type if scanresults[i].encrypted else "none", 'iface': self.iface, 'maxrate': scanresults[i].bitrates, 'mode': scanresults[i].mode, 'quality': scanresults[i].quality, 'signal': scanresults[i].signal, 'frequency': scanresults[i].frequency, 'frequency_norm': scanresults[i].frequency_norm, 'address': scanresults[i].address, 'noise': scanresults[i].noise, 'pairwise_ciphers': scanresults[i].pairwise_ciphers, 'authentication_suites': scanresults[i].authentication_suites, } return aps
def test_noise_no_data(self): cell = Cell.from_string(IWLIST_SCAN_NO_ENCRYPTION) self.assertEqual(cell.noise, None)
def test_wpa2(self): cell = Cell.from_string(IWLIST_SCAN_WPA2) self.assertTrue(cell.encrypted) self.assertEqual(cell.encryption_type, 'wpa2')
def test_blank_ssid(self): cell = Cell.from_string(NO_SSID_AT_ALL) self.assertEqual(None, cell.ssid)
def test_list_index_error(self): # https://github.com/rockymeza/wifi/issues/42 cell = Cell.from_string(LIST_INDEX_ERROR)
def test_absolute_quality(self): # https://github.com/rockymeza/wifi/pull/45 cell = Cell.from_string(ABSOLUTE_QUALITY) self.assertEqual(cell.quality, '38/100') self.assertEqual(cell.signal, -92)
def test_alternative_iwlist_output(self): # https://github.com/rockymeza/wifi/issues/12 cell = Cell.from_string(ALTERNATIVE_OUTPUT) self.assertEqual(cell.quality, '78/100') self.assertEqual(cell.signal, -92)
def test_noname_cell(self): cell = Cell.from_string(NONAME_WIRELESS_NETWORK) self.assertEqual(cell.ssid, '')
def test_wep(self): cell = Cell.from_string(IWLIST_SCAN_WEP) self.assertTrue(cell.encrypted) self.assertEqual(cell.encryption_type, 'wep')
def test_wpa1(self): cell = Cell.from_string(IWLIST_SCAN_WPA1) self.assertTrue(cell.encrypted) self.assertEqual(cell.encryption_type, 'wpa')
def test_pairwise_ciphers(self): cell = Cell.from_string(IWLIST_SCAN_WPA_WPA2_DUAL_CIPHERS) self.assertListEqual(['CCMP', 'TKIP'], cell.pairwise_ciphers)
def test_wpa_enterprise(self): cell = Cell.from_string(IWLIST_SCAN_WPA_ENTERPRISE) self.assertTrue(cell.encrypted) self.assertEqual('wpa-enterprise', cell.encryption_type)
def test_no_channel_output(self): cell = Cell.from_string(NO_CHANNEL_OUTPUT) self.assertEqual(11, cell.channel)
def test_noise_data_present(self): cell = Cell.from_string(LIST_INDEX_ERROR) self.assertEqual(-92, cell.noise)
def test_noise_no_data(self): cell = Cell.from_string(IWLIST_SCAN_NO_ENCRYPTION) self.assertEqual(None, cell.noise)
def test_blank_ssid(self): # https://github.com/rockymeza/wifi/issues/86 cell = Cell.from_string(NO_SSID_AT_ALL) self.assertEqual(cell.ssid, None)
def tick(): ''' ***REMOVE IF WE CAN TELL IF WE ARE CONNECTED If there is only 1 wifi network, it means that we are already connected or that there is only 1 wifi network. Do nothing since the user can try and connect to that network on there own. ''' ''' A list of the addresses of networks that are repetitive with each scan. Make the noted and previous network lists global ''' global prevNetworks global notedNetworks lastingNetworks = [] ''' Parse the data from the already parsed data of Cell.all("wlan0") and turn it into a list of the wifi networks each represented by the Network class. When parsing, if a network has the name of another in the list, keep/add whichever network has the strongest signal. If each have the same signal, choose the current network being iterated over. Ex: Two seperate xfinitywifi networks. ''' liveNetworks = [] for network in Cell.all("wlan0"): ssid = network.ssid address = network.address signal = round(int((network.quality.split("/")[0]))/.7) encrypted = network.encrypted parsedNetwork = Network(ssid,address,signal,encrypted) if (getNetworkFromSSID(liveNetworks, ssid) is not None): otherNetwork = getNetworkFromSSID(liveNetworks, ssid) if (otherNetwork.signal <= signal): removeNetwork(liveNetworks, ssid) liveNetworks.append(parsedNetwork) else: liveNetworks.append(parsedNetwork) ''' With our fresh set of live parsed networks, we now need to compare them with our older previous set of networks so that we can update the vital list of lasting networks. We also need to average out the signal from the previous signal and current signal for more accuracy and increase the amount of times this network has been scanned in a row if it appears in the list of old networks. We also need to add its MAC Address if it's not already in the lasting list of networks. ''' for network in liveNetworks: if (not(getNetworkFromMAC(prevNetworks,network.address) == None)): oldNetwork = getNetworkFromMAC(prevNetworks,network.address) network.count = (oldNetwork.count + 1) network.signal = round((network.signal + oldNetwork.signal) / 2) if (notedNetworks.count(network.address) == 0 and lastingNetworks.count(network.address) == 0): lastingNetworks.append(network.address) ''' Run through our list of lasting networks and check if each network has had an average signal of 50% or above and has been scanned 3 times in a row. If so, display the notification that the network is available and remove it from the list of lasting network MACs. Add it to the list of noted networks so it doesn't get thrown back in the mix. ''' ''' Remove noted network MAC addresses if they no longer are lasting ''' removedMACS = [] for address in lastingNetworks: network = getNetworkFromMAC(liveNetworks, address) if (network.signal >= 50 and network.count >= 3): removedMACS.append(address) if (notedNetworks.count(address) == 0): notedNetworks.append(address) ssid = network.ssid encrypted = None if (network.encrypted is True): encrypted = "Yes" else: encrypted = "No" if ("x00" in str(ssid)): ssid = "Unknown" pynotify.init("netscan - " + network.ssid) notification = pynotify.Notification("Network Detected", "SSID: " + ssid + "\n" + "Signal Strength: " + str(network.signal).replace(".0", "") + "%\n" + "Encrypted: " + encrypted, "/home/max/tools/icons/wifi.png") notification.show() for address in removedMACS: lastingNetworks.remove(address) prevNetworks = liveNetworks
def test_signal_level_out_of_sixty(self): cell = Cell.from_string(ALTERNATIVE_OUTPUT2) self.assertEqual(cell.signal, -71)
def test_no_channel_output(self): # https://github.com/rockymeza/wifi/issues/24 cell = Cell.from_string(NO_CHANNEL_OUTPUT) self.assertEqual(cell.channel, 11)
def iwconfigFinished(self, result, retval, extra_args): result = six.ensure_str(result) iface = extra_args ssid = "off" data = { 'essid': False, 'frequency': False, 'accesspoint': False, 'bitrate': False, 'encryption': False, 'quality': False, 'signal': False, 'channel': False, 'encryption_type': False, 'frequency': False, 'frequency_norm': False } for line in result.splitlines(): line = line.strip() if "ESSID" in line: if "off/any" in line: ssid = "off" else: if "Nickname" in line: ssid = (line[line.index('ESSID') + 7:line.index('" Nickname')]) else: ssid = (line[line.index('ESSID') + 7:len(line) - 1]) if ssid != "off": data['essid'] = ssid if "Access Point" in line: if "Sensitivity" in line: ap = line[line.index('Access Point') + 14:line.index(' Sensitivity')] else: ap = line[line.index('Access Point') + 14:len(line)] if ap is not None: data['accesspoint'] = ap if "Frequency" in line: frequency = line[line.index('Frequency') + 10:line.index(' GHz')] if frequency is not None: data['frequency'] = frequency if "Bit Rate" in line: if "kb" in line: br = line[line.index('Bit Rate') + 9:line.index(' kb/s')] elif "Gb" in line: br = line[line.index('Bit Rate') + 9:line.index(' Gb/s')] else: br = line[line.index('Bit Rate') + 9:line.index(' Mb/s')] if br is not None: data['bitrate'] = br if ssid != None and ssid != "off": scanresults = list(Cell.all(iface)) aps = {} if scanresults is not None: for i in range(len(scanresults)): bssid = scanresults[i].ssid aps[bssid] = { 'active': True, 'bssid': scanresults[i].ssid, 'essid': scanresults[i].ssid, 'channel': scanresults[i].channel, 'encrypted': scanresults[i].encrypted, 'encryption_type': scanresults[i].encryption_type if scanresults[i].encrypted else "none", 'iface': iface, 'maxrate': scanresults[i].bitrates, 'mode': scanresults[i].mode, 'quality': scanresults[i].quality, 'signal': scanresults[i].signal, 'frequency': scanresults[i].frequency, 'frequency_norm': scanresults[i].frequency_norm, 'address': scanresults[i].address, 'noise': scanresults[i].noise, 'pairwise_ciphers': scanresults[i].pairwise_ciphers, 'authentication_suites': scanresults[i].authentication_suites, } #data['bitrate'] = aps[ssid]["maxrate"] data['encryption'] = aps[ssid]["encrypted"] data['quality'] = aps[ssid]["quality"] data['signal'] = aps[ssid]["signal"] data['channel'] = aps[ssid]["channel"] data['encryption_type'] = aps[ssid]["encryption_type"] #data['frequency'] = aps[ssid]["frequency"] data['frequency_norm'] = aps[ssid]["frequency_norm"] self.wlaniface[iface] = data self.backupwlaniface = self.wlaniface if self.WlanConsole is not None: if not self.WlanConsole.appContainers: print("[Wlan.py] self.wlaniface after loading:", self.wlaniface) if self.statusCallback is not None: self.statusCallback(True, self.wlaniface) self.statusCallback = None
def test_frequency_no_channel_output(self): # https://github.com/rockymeza/wifi/issues/39 cell = Cell.from_string(FREQUENCY_NO_CHANNEL_OUTPUT) self.assertEqual(cell.channel, 149)
def test_frequency_no_channel_output(self): cell = Cell.from_string(FREQUENCY_NO_CHANNEL_OUTPUT) self.assertEqual(149, cell.channel)
def test_noise_data_present(self): cell = Cell.from_string(LIST_INDEX_ERROR) self.assertEqual(cell.noise, -92)
def test_absolute_quality(self): # https://github.com/rockymeza/wifi/pull/45 cell = Cell.from_string(ABSOLUTE_QUALITY) self.assertEqual('38/100', cell.quality) self.assertEqual(-92, cell.signal)
def test_wpa_wpa2(self): cell = Cell.from_string(IWLIST_SCAN_WPA_WPA2) self.assertTrue(cell.encrypted) self.assertEqual('wpa/wpa2', cell.encryption_type)
def test_list_index_error(self): cell = Cell.from_string(LIST_INDEX_ERROR)