Example #1
0
def setup_card(interface_name, frequency, data_rate=2):
    print("Settings up " + interface_name)
    wifi_card = pyw.getcard(interface_name)
    driver_name = iwhw.ifdriver(interface_name)
    if driver_name in EXPERIMENTAL_DRIVERS:
        print("Warning: Using WiFi adapter with experimental support!")
    print("Setting " + wifi_card.dev + " " + driver_name + " " + str(frequency) + " MHz" +
          " bitrate: " + get_bit_rate(data_rate) + " Mbps")
    if not pyw.isup(wifi_card):
        print("\tup...")
        pyw.up(wifi_card)
    if is_atheros_card(driver_name):
        # for all other cards the transmission rate is set via the radiotap header
        set_bitrate(interface_name, data_rate)
    if pyw.isup(wifi_card):
        print("\tdown...")
        pyw.down(wifi_card)
    print("\tmonitor...")
    pyw.modeset(wifi_card, 'monitor')
    if is_realtek_card(driver_name):
        # Other cards power settings are set via e.g. 'txpower_atheros 58' or 'txpower_ralink 0' (defaults)
        pyw.txset(wifi_card, 'fixed', 3000)
    if not pyw.isup(wifi_card):
        print("\tup...")
        pyw.up(wifi_card)
    print("\tfrequency...")
    pyw.freqset(wifi_card, frequency)
    print("\tMTU...")
    if is_realtek_card(driver_name):
        subprocess.run(['ip link set dev ' + interface_name + ' mtu 1500'], shell=True)
    else:
        subprocess.run(['ip link set dev ' + interface_name + ' mtu 2304'], shell=True)
    pyw.regset('DE')  # to allow channel 12 and 13 for hotspot
    rename_interface(interface_name)
    def set_tx_power(self, power_dBm, ifaceName):
        """
        Sets TX power for that interface
        :param power_dBm: the power in dBm
        :param ifaceName: name of interface
        :return: True in case it was successful
        """

        self.log.info('Setting power on iface {}:{} to {}'.format(
            ifaceName, self.device, str(power_dBm)))
        try:
            w0 = self.get_wifi_chard(ifaceName)  # get a card for interface
            pyw.txset(w0, 'fixed', power_dBm)
            self.power = power_dBm
        except Exception as e:
            raise exceptions.FunctionExecutionFailed(
                func_name=inspect.currentframe().f_code.co_name,
                err_msg='Failed to set tx power: ' + str(e))

        return True
Example #3
0
    def configure_interface(self,
                            interface,
                            name='mon0',
                            channel=1,
                            txpower=60,
                            bitrate=11):
        """Configure the given card in monitor mode"""

        # Determine the type of card for this interface
        try:
            driver = pywhw.ifcard(interface)[0]
            print(driver)
            if driver == 'rtl88xxau':
                type = Card.rtl88xx
            else:
                type = Card.ath9k
        except Exception as e:
            print(e)
            return None

        # Get the card for this interface
        try:
            card = pyw.getcard(interface)
        except pyric.error as e:
            logging.error("Error connecting to the interface: " + interface)
            return None

        # Ensure this card supports monitor mode
        if 'monitor' not in pyw.devmodes(card):
            logging.error(interface + " does not support monitor mode")
            return None

        # Configure the bitrate for this card
        # This is not supported by pyric, so we have to do it manually.
        if bitrate != 0 and type == Card.ath9k:
            try:
                pyw.down(card)
                pyw.modeset(card, 'managed')
                pyw.up(card)
                logging.debug("Setting the bitrate on interface " + interface +
                              " to " + str(bitrate))
                if os.system("iw dev " + card.dev +
                             " set bitrates legacy-2.4 " + str(bitrate)) != 0:
                    #if os.system("iwconfig " + card.dev + " rate 54M fixed") != 0:
                    logging.error("Error setting the bitrate for: " +
                                  interface)
                    return None
                pyw.down(card)
            except pyric.error as e:
                logging.error("Error setting the bitrate for: " + interface)
                logging.error(e)
                return None

        # Try to configure the transmit power level (some cards don't support this)
        try:
            pyw.txset(card, txpower, 'fixed')
        except pyric.error as e:
            pass

        # Bring the interface up
        try:
            pyw.up(card)
        except pyric.error as e:
            logging.error("Error bringing up the interface: " + card.dev)
            logging.error(e)
            return False

        # Configure the channel
        try:
            logging.debug("Changing to channel: " + str(channel))
            pyw.chset(card, channel, None)

        except pyric.error as e:
            logging.error("Error setting the wifi channel on: " + card.dev)
            logging.error(e)
            return False

        return card
 def set_txpower(self, dbm):
     pyw.txset(self.card, 'fixed', dbm)
Example #5
0
 def set_txpower(self, dbm):
     if self._verify_card():
         pyw.txset(self.card, 'fixed', dbm)
 def set_txpower(self, dbm):
     if self._verify_card():
         pyw.txset(self.card, 'fixed', dbm)