Example #1
0
 def test_db2dbm(self):
     self.assertEqual(db2dbm(-10), -100)
     self.assertEqual(db2dbm(0), -100)
     self.assertEqual(db2dbm(1), -99)
     self.assertEqual(db2dbm(2), -99)
     self.assertEqual(db2dbm(50), -75)
     self.assertEqual(db2dbm(99), -50)
     self.assertEqual(db2dbm(100), -50)
     self.assertEqual(db2dbm(101), -50)
     self.assertEqual(db2dbm(200), -50)
Example #2
0
 def test_db2dbm(self):
     self.assertEqual(db2dbm(-10), -100)
     self.assertEqual(db2dbm(0), -100)
     self.assertEqual(db2dbm(1), -99)
     self.assertEqual(db2dbm(2), -99)
     self.assertEqual(db2dbm(50), -75)
     self.assertEqual(db2dbm(99), -50)
     self.assertEqual(db2dbm(100), -50)
     self.assertEqual(db2dbm(101), -50)
     self.assertEqual(db2dbm(200), -50)
Example #3
0
File: scan.py Project: Rondom/wifi
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
Example #4
0
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
Example #5
0
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()
    logger.debug('Starting cell block with %d lines', len(lines))

    for line in lines:
        if line.startswith('Quality'):
            logger.debug('- Quality line')
            for re_name, quality_re in quality_re_dict.items():
                match_result = quality_re.search(line)
                if match_result is not None:
                    groups = match_result.groupdict()
                    cell.quality = groups['quality']
                    signal = groups['siglevel']
                    noise = groups.get('noiselevel')
                    if re_name == 'relative':
                        actual, total = map(int, signal.split('/'))
                        cell.signal = db2dbm(int((actual / total) * 100))
                    elif re_name == 'absolute':
                        cell.quality += '/100'
                        cell.signal = db2dbm(int(signal))
                    else:
                        cell.signal = int(signal)
                    if noise is not None:
                        cell.noise = int(noise)
                    break

        elif line.startswith('Bit Rates'):
            logger.debug('- Bit rates line')
            values = split_on_colon(line)[1].split('; ')

            # consume next line of bit rates, because they are split on
            # different lines, sometimes...
            if lines:
                while lines[0].startswith(' ' * 10):
                    values += lines.pop(0).strip().split('; ')

            cell.bitrates.extend(values)

        elif line.startswith('    Group Cipher'):
            logger.debug('- Group Cipher line')
            __, cell.group_cipher = split_on_colon(line)

        elif line.startswith('    Pairwise Ciphers'):
            logger.debug('- Pairwise ciphers line')
            key, value = split_on_colon(line)
            cell.pairwise_ciphers = value.split(' ')

        elif line.startswith('    Authentication Suites'):
            logger.debug('- Authentication suites line')
            key, value = split_on_colon(line)  # type: str, str
            if '802.1x' in value and cell.encryption_type == 'wpa2':
                cell.encryption_type = 'wpa2-enterprise'
            elif '802.1x' in value and cell.encryption_type == 'wpa':
                cell.encryption_type = 'wpa-enterprise'
            cell.authentication_suites = value.split(' ')

        elif ':' in line:
            logger.debug('- Line with colon')
            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 and cell.encryption_type is None:
                    cell.encryption_type = 'wpa2'
                elif 'WPA' in value and cell.encryption_type is None:
                    cell.encryption_type = 'wpa'
                elif 'WPA' in value and cell.encryption_type == 'wpa2' or \
                        'WPA2' in value and cell.encryption_type == 'wpa':
                    cell.encryption_type = 'wpa/wpa2'

            if key == 'frequency':
                matches = frequency_re.search(value)
                cell.frequency = matches.group('frequency')
                f, __ = cell.frequency.split(' ', 1)
                f = float(f)
                if LOWBOUND_FREQ_24 <= f <= UPPERBOUND_FREQ_24:
                    cell.frequency_norm = '2.4Ghz'
                elif LOWBOUND_FREQ_5 <= f <= UPPERBOUND_FREQ_5:
                    cell.frequency_norm = '5Ghz'
                if matches.group('channel'):
                    cell.channel = int(matches.group('channel'))
            elif key in normalize_value:
                setattr(cell, key, normalize_value[key](value))
        else:
            logger.debug('- Line of no interest: "%s"', line.strip())

    # It seems that encryption types other than WEP need to specify their
    # existence.
    if cell.encrypted and not cell.encryption_type:
        logger.debug('Defaulting to WEP')
        cell.encryption_type = 'wep'

    return cell
Example #6
0
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 = list(map(int, signal.split("/")))
                        cell.signal = db2dbm(int((actual / total) * 100))
                    elif re_name == "absolute":
                        cell.quality = cell.quality + "/100"
                        cell.signal = db2dbm(int(signal))
                    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...
            if lines:
                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":
                matches = frequency_re.search(value)
                cell.frequency = matches.group("frequency")
                if matches.group("channel"):
                    cell.channel = int(matches.group("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