def _check_if_my_iface(self, ifaceName): if not pyw.iswireless(ifaceName): return False myIfaces = self._get_my_ifaces() if ifaceName in myIfaces: return True return False
def DebuggingInterface(iface): interfaces = pyw.interfaces() print(interfaces) print(("Is %s an interface? %s" % (iface, pyw.isinterface(iface)))) print(("Is %s a wireless device? %s" % (iface, pyw.iswireless(iface)))) w0 = pyw.getcard(iface) print(("Is %s active? %s" % (iface, pyw.isup(w0)))) print(("Is %s blocked? %s" % (iface, pyw.isblocked(w0)))) iinfo = pyw.ifinfo(w0) print(iinfo) pinfo = pyw.phyinfo(w0) print((pinfo['bands']))
def is_wireless_interface(interface_name): """ Check if the interface is wireless interface :param interface_name: Name of an interface :type interface_name: str :return: True if the interface is wireless interface :rtype: bool """ if pyw.iswireless(interface_name): return True return False
def listInterfaces(prefix=""): """ Returns a list of all wired interfaces in the system except 'lo'. Arguments: prefix -- list only those interface which starts with the given prefix. """ ifaces = netifaces.interfaces() result = [] # check for wired deives with defined prefix for iname in ifaces: tmp = str(iname) if tmp != "lo" and pyw.iswireless(tmp) == False and tmp.startswith( prefix): result.append(tmp) return result
def get_interfaces(self): # request list of interfaces from pyric all_interfaces = pyw.interfaces() #walk all interfaces for interface in all_interfaces: # check if the interface is loopback or virtual if get_mac_address( interface) != "00:00:00:00:00" and interface != "lo": # check if it's wireless or not if pyw.iswireless(interface): self.wireless_interfaces.append(interface) else: self.wired_interfaces.append(interface) # append a placeholder interaface for the empty lists if len(self.wireless_interfaces) == 0: self.wireless_interfaces.append("None") if len(self.wired_interfaces) == 0: self.wired_interfaces.append("None")
def interface_property_detector(network_adapter): """ Add appropriate properties of the interface such as supported modes and wireless type(wireless) :param network_adapter: A NetworkAdapter object :type interface_name: NetworkAdapter :return: None :rtype: None """ supported_modes = pyw.devmodes(network_adapter.card) # check for monitor, AP and wireless mode support if "monitor" in supported_modes: network_adapter.has_monitor_mode = True if "AP" in supported_modes: network_adapter.has_ap_mode = True if pyw.iswireless(network_adapter.name): network_adapter.is_wireless = True
def createVirtualInterface(old_iface, new_iface, mode="monitor", channel=1): """ Creates a virtual interface with the specified options and returns its pywric.Card object. (when creating new interface the old one is deleted.) Arguments : old_iface -- old interface name. new_iface -- new interface name. mode -- open the new interface in the given mode (default:monitor). channel -- start the new interface on the given channel. """ # return None if invailed wireless interface if pyw.iswireless(old_iface) == False: return None wi = pyw.getcard(old_iface) # check if the specifed mode is supported by the card if mode in pyw.devmodes(wi): # create new interfaces with the specifed prefix-string default="mon" viface = pyw.devadd(wi, new_iface, mode) # delete all other interfaces with same phy id for card, _ in pyw.ifaces(wi): # delete all interfaces if not card.dev == viface.dev: # that are not our pyw.devdel(card) # set default channel pyw.chset(viface, channel, None) # up the vitual interface pyw.up(viface) # return the newly created interface as pyw.Card() onject return viface
def set_internet_iface(self, iface): if pyw.iswireless(iface): raise Exception self.internet_iface = iface
def is_add_vif_required(args): """ Return the card if only that card support both monitor and ap :param args: Arguemnt from pywifiphisher :type args: parse.args :return: tuple of card and is_frequency_hop_allowed :rtype: tuple """ def get_perfect_card(phy_map_vifs, vif_score_tups): """ Get the perfect card that both supports ap and monitor when we have only one phy interface can do that :param phy_map_vifs: phy number maps to the virtual interfaces :param vif_score_tups: list of tuple containing card and score :type phy_map_vifs: dict :type vif_score_tups: list :return tuple of card and single_perfect_phy_case :rtype: tuple """ # case 1 : one phy maps to one virtual interface if len(phy_map_vifs) == 1 and len(phy_map_vifs.values()[0]) == 1: # only take the first tuple vif_score_tuple = vif_score_tups[0] card = vif_score_tuple[0] score = vif_score_tuple[1] # if this card support both monitor and AP mode if score == 2: return card, True # case 2 : one phy maps to multiple virtual interfaces # we don't need to create one more virtual interface in this case elif len(phy_map_vifs) == 1 and len(phy_map_vifs.values()[0]) > 1: return None, True # case 3 : we have multiple phy interfaces but only # one card support both monitor and AP and the other # ones just support the managed mode only elif len(phy_map_vifs) > 1: if vif_score_tups[0][1] == 2 and vif_score_tups[1][1] == 0: return vif_score_tups[0][0], True return None, False # map the phy interface to virtual interfaces # i.e. phy0 to wlan0 phy_to_vifs = defaultdict(list) # store the phy number for the internet access invalid_phy_number = None # record the invalid_phy_number when it is wireless card if args.internetinterface and pyw.iswireless(args.internetinterface): card = pyw.getcard(args.internetinterface) invalid_phy_number = card.phy # map the phy# to the virtual interface tuples for vif in [vif for vif in pyw.interfaces() if pyw.iswireless(vif)]: # excluding the card that used for internet accessing # setup basic card information score = 0 card = pyw.getcard(vif) phy_number = card.phy if phy_number == invalid_phy_number: continue supported_modes = pyw.devmodes(card) if "monitor" in supported_modes: score += 1 if "AP" in supported_modes: score += 1 phy_to_vifs[phy_number].append((card, score)) # each phy number map to a sublist containing (card, score) vif_score_tuples = [sublist[0] for sublist in phy_to_vifs.values()] # sort with score vif_score_tuples = sorted(vif_score_tuples, key=lambda tup: -tup[1]) perfect_card, is_single_perfect_phy = get_perfect_card(phy_to_vifs, vif_score_tuples) return perfect_card, is_single_perfect_phy
def main(filename, interface, ssid, channel, output, wpa_passphrase, sync, set_country_code): """Console script for wifi_configurator.""" config = hostapd_conf_as_config(filename) # Could use a callback and access filename parameter in the decorator, # (filename possibly needs is_eager) or by subclassing click.Option if not ssid: ssid = get_current_ssid(config) wifi_adapter = adapters.factory(interface) # We deliberately only instantiate pyw.getcard for as small a set of # parameters as possible because pyw gets sad if operations are # attempted on a device that does not support nl80211 and we want to # be to do as many things as possible in simulations scan_output = "" if set_country_code or not channel: try: if pyw.iswireless(interface): wlan_if = pyw.getcard(interface) scan_output = scan.get_scan_output(wlan_if) else: click.echo("Interface %s is not a wifi interface. Won't be " "able to infer country code or do automatic " "channel selection" % (interface, )) except pyric.error: click.echo("Unable to query interface %s with pyw. Won't be " "able to infer country code or do automatic " "channel selection" % (interface, )) # Retrieve the previous cc now, given we have so many fallback cases country_code = get_current_country_code(config) if set_country_code: scanned_cc = scan.detect_regdomain(scan_output) # Only use the scanned cc if it's non-empty if scanned_cc: country_code = scanned_cc else: click.echo("Could not do wifi scan. Using previous country code") valid_channels_for_cc = scan.channels_for_country(country_code) if not channel: # Choose an uncontested channel, or a random one if there aren't any # MEH that we respecify wlan_if when we've used it to scan just a bit # earlier channel = scan.get_available_uncontested_channel( valid_channels_for_cc, scan_output) if not channel: channel = random.choice(valid_channels_for_cc) click.echo("No uncontested channels. Choosing %s at random" % (channel, )) else: click.echo("Channel %s is uncontested and is now the new channel" % (channel, )) channel = get_current_channel(config) if channel not in valid_channels_for_cc: click.echo("Channel %s is not valid for new country code %s. " "Setting channel to %s" % (channel, country_code, valid_channels_for_cc[0])) channel = valid_channels_for_cc[0] else: if channel not in valid_channels_for_cc: raise click.BadParameter( "Channel %s is not valid for country code %s. Valid choices " "are %s. Exiting" % (channel, country_code, ",".join( [str(i) for i in valid_channels_for_cc]))) file_loader = jinja2.PackageLoader('wifi_configurator') env = jinja2.Environment( loader=file_loader, trim_blocks=True, lstrip_blocks=True, ) template = env.get_template('hostapd.conf.j2') rendered = template.stream( interface=interface, ssid=ssid, channel=channel, country_code=country_code, wifi_adapter=wifi_adapter, wpa_passphrase=wpa_passphrase, ) rendered.dump(output) # Some filesystems don't write to disk for a while, which can lead to # corruption in files. A corrupt hostapd.conf may well brick a device # in the field so let's avoid that risk. # Related: https://github.com/ConnectBox/connectbox-pi/issues/220 if sync and output != sys.stdout: subprocess.run("/bin/sync") return 0
def is_add_vif_required(args): """ Return the card if only that card support both monitor and ap :param args: Arguemnt from pywifiphisher :type args: parse.args :return: tuple of card and is_frequency_hop_allowed :rtype: tuple """ def get_perfect_card(phy_map_vifs, vif_score_tups): """ Get the perfect card that both supports ap and monitor when we have only one phy interface can do that :param phy_map_vifs: phy number maps to the virtual interfaces :param vif_score_tups: list of tuple containing card and score :type phy_map_vifs: dict :type vif_score_tups: list :return tuple of card and single_perfect_phy_case :rtype: tuple """ # case 1 : one phy maps to one virtual interface if len(phy_map_vifs) == 1 and len(phy_map_vifs.values()[0]) == 1: # only take the first tuple vif_score_tuple = vif_score_tups[0] card = vif_score_tuple[0] score = vif_score_tuple[1] # if this card support both monitor and AP mode if score == 2: return card, True # case 2 : one phy maps to multiple virtual interfaces # we don't need to create one more virtual interface in this case elif len(phy_map_vifs) == 1 and len(phy_map_vifs.values()[0]) > 1: return None, True # case 3 : we have multiple phy interfaces but only # one card support both monitor and AP and the other # ones just support the managed mode only elif len(phy_map_vifs) > 1: if vif_score_tups[0][1] == 2 and vif_score_tups[1][1] == 0: return vif_score_tups[0][0], True return None, False # map the phy interface to virtual interfaces # i.e. phy0 to wlan0 phy_to_vifs = defaultdict(list) # store the phy number for the internet access invalid_phy_number = list() # record the invalid_phy_number when it is wireless card if args.internetinterface and pyw.iswireless(args.internetinterface): card = pyw.getcard(args.internetinterface) invalid_phy_number.append(card.phy) if args.wpspbc_assoc_interface: card = pyw.getcard(args.wpspbc_assoc_interface) invalid_phy_number.append(card.phy) # map the phy# to the virtual interface tuples for vif in [vif for vif in pyw.interfaces() if pyw.iswireless(vif)]: # excluding the card that used for internet accessing # setup basic card information score = 0 card = pyw.getcard(vif) phy_number = card.phy if phy_number in invalid_phy_number: continue supported_modes = pyw.devmodes(card) if "monitor" in supported_modes: score += 1 if "AP" in supported_modes: score += 1 phy_to_vifs[phy_number].append((card, score)) # each phy number map to a sublist containing (card, score) vif_score_tuples = [sublist[0] for sublist in phy_to_vifs.values()] # sort with score vif_score_tuples = sorted(vif_score_tuples, key=lambda tup: -tup[1]) use_one_phy = False if args.interface: card = pyw.getcard(args.interface) phy_number = card.phy if phy_to_vifs[card.phy][0][1] == 2: perfect_card = card use_one_phy = True else: perfect_card, use_one_phy = get_perfect_card( phy_to_vifs, vif_score_tuples) return perfect_card, use_one_phy
def test_not_iswinterface(self): for nic in inics + enics: self.assertFalse(pyw.iswireless(nic))
def test_iswinterface(self): for wnic in pyw.winterfaces(): self.assertTrue(pyw.iswireless(wnic))