'Scans for access points. Asks user to select target(s).
"channel" - the channel to scan on, 0 scans all channels.
"iface"   - the interface to scan on. must be a real interface.
"tried_rtl8187_fix" - We have already attempted to fix "Unknown error 132"
Returns list of selected targets and list of clients.'

def scan(self, channel=0, iface='', tried_rtl8187_fix=False):

    remove_airodump_files((self.RUN_CONFIG.temp + 'wifite'))
    command = ['airodump-ng', '-a', '--write-interval', '1', '-w', (self.RUN_CONFIG.temp + 'wifite')]
    if (channel != 0):
        command.append('-c')
        command.append(str(channel))
    command.append(iface)
    proc = Popen(command, stdout=DN, stderr=DN)
    time_started = time.time()
    print (((((((((((((GR + '    [+]    ') + G) + 'initializing    scan') + W) + '    (') + G) + iface) + W) + '),    updates    at    1    sec    intervals,    ') + G) + 'CTRL+C') + W) + '    when    ready.')
    (targets, clients) = ([], [])
    try:
        deauth_sent = 0.0
        old_targets = []
        stop_scanning = False
        while True:
            time.sleep(0.3)
            if ((not os.path.exists((self.RUN_CONFIG.temp + 'wifite-01.csv'))) and ((time.time() - time_started) > 1.0)):
                print ((R + '\n    [!]    ERROR!') + W)
                if (proc.poll() is not None):
                    proc = Popen(['airodump-ng', iface], stdout=DN, stderr=PIPE)
                    if ((not tried_rtl8187_fix) and (proc.communicate()[1].find('failed:    Unknown    error    132') != (-1))):
                        send_interrupt(proc)
                        if self.rtl8187_fix(iface):
                            return self.scan(channel=channel, iface=iface, tried_rtl8187_fix=True)
                print ((((R + '    [!]') + O) + '    wifite    is    unable    to    generate    airodump-ng    output    files') + W)
                print ((((R + '    [!]') + O) + '    you    may    want    to    disconnect/reconnect    your    wifi    device') + W)
                self.RUN_CONFIG.exit_gracefully(1)
            (targets, clients) = self.parse_csv((self.RUN_CONFIG.temp + 'wifite-01.csv'))
            if (self.RUN_CONFIG.SHOW_ALREADY_CRACKED == False):
                index = 0
                while (index < len(targets)):
                    already = False
                    for cracked in self.RUN_CONFIG.CRACKED_TARGETS:
                        if (targets[index].ssid.lower() == cracked.ssid.lower()):
                            already = True
                        if (targets[index].bssid.lower() == cracked.bssid.lower()):
                            already = True
                    if (already == True):
                        targets.pop(index)
                        index -= 1
                    index += 1
            if (self.RUN_CONFIG.TARGET_ESSID != ''):
                for t in targets:
                    if (t.ssid.lower() == self.RUN_CONFIG.TARGET_ESSID.lower()):
                        send_interrupt(proc)
                        try:
                            os.kill(proc.pid, SIGTERM)
                        except OSError:
                            pass
                        except UnboundLocalError:
                            pass
                        targets = [t]
                        stop_scanning = True
                        break
            if (self.RUN_CONFIG.TARGET_BSSID != ''):
                for t in targets:
                    if (t.bssid.lower() == self.RUN_CONFIG.TARGET_BSSID.lower()):
                        send_interrupt(proc)
                        try:
                            os.kill(proc.pid, SIGTERM)
                        except OSError:
                            pass
                        except UnboundLocalError:
                            pass
                        targets = [t]
                        stop_scanning = True
                        break
            if (self.RUN_CONFIG.ATTACK_ALL_TARGETS and ((time.time() - time_started) > 10)):
                print (((GR + '\n    [+]') + W) + ('    auto-targeted    %s%d%s    access    point%s' % (G, len(targets), W, ('' if (len(targets) == 1) else 's'))))
                stop_scanning = True
            if ((self.RUN_CONFIG.ATTACK_MIN_POWER > 0) and ((time.time() - time_started) > 10)):
                i = 0
                before_count = len(targets)
                while (i < len(targets)):
                    if (targets[i].power < self.RUN_CONFIG.ATTACK_MIN_POWER):
                        targets.pop(i)
                    else:
                        i += 1
                print (((GR + '\n    [+]') + W) + ('    removed    %s    targets    with    power    <    %ddB,    %s    remain' % (((G + str((before_count - len(targets)))) + W), self.RUN_CONFIG.ATTACK_MIN_POWER, ((G + str(len(targets))) + W))))
                stop_scanning = True
            if stop_scanning:
                break
            if (self.RUN_CONFIG.SEND_DEAUTHS and (channel != 0) and ((time.time() - deauth_sent) > 5)):
                deauth_sent = time.time()
                for t in targets:
                    if (t.ssid == ''):
                        print ('\r    %s    deauthing    hidden    access    point    (%s)                                                            \r' % (((GR + sec_to_hms((time.time() - time_started))) + W), ((G + t.bssid) + W))),
                        stdout.flush()
                        cmd = ['aireplay-ng', '--ignore-negative-one', '--deauth', str(self.RUN_CONFIG.WPA_DEAUTH_COUNT), '-a', t.bssid]
                        for c in clients:
                            if (c.station == t.bssid):
                                cmd.append('-c')
                                cmd.append(c.bssid)
                                break
                        cmd.append(iface)
                        proc_aireplay = Popen(cmd, stdout=DN, stderr=DN)
                        proc_aireplay.wait()
                        time.sleep(0.5)
                    else:
                        for ot in old_targets:
                            if ((ot.ssid == '') and (ot.bssid == t.bssid)):
                                print ('\r    %s    successfully    decloaked    "%s"                                                                                    ' % (((GR + sec_to_hms((time.time() - time_started))) + W), ((G + t.ssid) + W)))
                old_targets = targets[:]
            if (self.RUN_CONFIG.VERBOSE_APS and (len(targets) > 0)):
                targets = sorted(targets, key=(lambda t: t.power), reverse=True)
                if (not self.RUN_CONFIG.WPS_DISABLE):
                    wps_check_targets(targets, (self.RUN_CONFIG.temp + 'wifite-01.cap'), verbose=False)
                os.system('clear')
                print (((((((((((((GR + '\n    [+]    ') + G) + 'scanning') + W) + '    (') + G) + iface) + W) + '),    updates    at    1    sec    intervals,    ') + G) + 'CTRL+C') + W) + '    when    ready.\n')
                print ('            NUM    ESSID                                                                    %sCH        ENCR        POWER        WPS?        CLIENT' % ('BSSID                                                        ' if self.RUN_CONFIG.SHOW_MAC_IN_SCAN else ''))
                print ('            ---    --------------------        %s--        ----        -----        ----        ------' % ('-----------------        ' if self.RUN_CONFIG.SHOW_MAC_IN_SCAN else ''))
                for (i, target) in enumerate(targets):
                    print ('            %s%2d%s    ' % (G, (i + 1), W)),
                    if (target.ssid == ''):
                        p = ((((((O + '(') + target.bssid) + ')') + GR) + '    ') + W)
                        print ('%s' % p.ljust(20)),
                    elif (target.ssid.count('\x00') == len(target.ssid)):
                        p = (('<Length    ' + str(len(target.ssid))) + '>')
                        print ((('%s' % C) + p.ljust(20)) + W),
                    elif (len(target.ssid) <= 20):
                        print ((('%s' % C) + target.ssid.ljust(20)) + W),
                    else:
                        print (((('%s' % C) + target.ssid[0:17]) + '...') + W),
                    if self.RUN_CONFIG.SHOW_MAC_IN_SCAN:
                        print O, (target.bssid + W),
                    print (G + target.channel.rjust(3)), W,
                    if (target.encryption.find('WEP') != (-1)):
                        print G,
                    else:
                        print O,
                    print (('\x08%3s' % target.encryption.strip().ljust(4)) + W),
                    if (target.power >= 55):
                        col = G
                    elif (target.power >= 40):
                        col = O
                    else:
                        col = R
                    print ('%s%3ddb%s' % (col, target.power, W)),
                    if self.RUN_CONFIG.WPS_DISABLE:
                        print ('        %3s' % ((O + 'n/a') + W)),
                    else:
                        print ('        %3s' % (((G + 'wps') + W) if target.wps else ((R + '    no') + W))),
                    client_text = ''
                    for c in clients:
                        if (c.station == target.bssid):
                            if (client_text == ''):
                                client_text = 'client'
                            elif (client_text[(-1)] != 's'):
                                client_text += 's'
                    if (client_text != ''):
                        print ('        %s' % ((G + client_text) + W))
                    else:
                        print ''
                print ''
            print ('    %s    %s    wireless    networks.    %s    target%s    and    %s    client%s    found            \r' % (((GR + sec_to_hms((time.time() - time_started))) + W), ((G + 'scanning') + W), ((G + str(len(targets))) + W), ('' if (len(targets) == 1) else 's'), ((G + str(len(clients))) + W), ('' if (len(clients) == 1) else 's'))),
            stdout.flush()
    except KeyboardInterrupt:
        pass
    print ''
    send_interrupt(proc)
    try:
        os.kill(proc.pid, SIGTERM)
    except OSError:
        pass
    except UnboundLocalError:
        pass
    if (not self.RUN_CONFIG.WPS_DISABLE):
        wps_check_targets(targets, (self.RUN_CONFIG.temp + 'wifite-01.cap'))
    remove_airodump_files((self.RUN_CONFIG.temp + 'wifite'))
    if stop_scanning:
        return (targets, clients)
    print ''
    if (len(targets) == 0):
        print ((((R + '    [!]') + O) + '    no    targets    found!') + W)
        print ((((R + '    [!]') + O) + '    you    may    need    to    wait    for    targets    to    show    up.') + W)
        print ''
        self.RUN_CONFIG.exit_gracefully(1)
    if self.RUN_CONFIG.VERBOSE_APS:
        os.system('clear')
    targets = sorted(targets, key=(lambda t: t.power), reverse=True)
    victims = []
    print ('            NUM    ESSID                                                                    %sCH        ENCR        POWER        WPS?        CLIENT' % ('BSSID                                                        ' if self.RUN_CONFIG.SHOW_MAC_IN_SCAN else ''))
    print ('            ---    --------------------        %s--        ----        -----        ----        ------' % ('-----------------        ' if self.RUN_CONFIG.SHOW_MAC_IN_SCAN else ''))
    for (i, target) in enumerate(targets):
        print ('            %s%2d%s    ' % (G, (i + 1), W)),
        if (target.ssid == ''):
            p = ((((((O + '(') + target.bssid) + ')') + GR) + '    ') + W)
            print ('%s' % p.ljust(20)),
        elif (target.ssid.count('\x00') == len(target.ssid)):
            p = (('<Length    ' + str(len(target.ssid))) + '>')
            print ((('%s' % C) + p.ljust(20)) + W),
        elif (len(target.ssid) <= 20):
            print ((('%s' % C) + target.ssid.ljust(20)) + W),
        else:
            print (((('%s' % C) + target.ssid[0:17]) + '...') + W),
        if self.RUN_CONFIG.SHOW_MAC_IN_SCAN:
            print O, (target.bssid + W),
        print (G + target.channel.rjust(3)), W,
        if (target.encryption.find('WEP') != (-1)):
            print G,
        else:
            print O,
        print (('\x08%3s' % target.encryption.strip().ljust(4)) + W),
        if (target.power >= 55):
            col = G
        elif (target.power >= 40):
            col = O
        else:
            col = R
        print ('%s%3ddb%s' % (col, target.power, W)),
        if self.RUN_CONFIG.WPS_DISABLE:
            print ('        %3s' % ((O + 'n/a') + W)),
        else:
            print ('        %3s' % (((G + 'wps') + W) if target.wps else ((R + '    no') + W))),
        client_text = ''
        for c in clients:
            if (c.station == target.bssid):
                if (client_text == ''):
                    client_text = 'client'
                elif (client_text[(-1)] != 's'):
                    client_text += 's'
        if (client_text != ''):
            print ('        %s' % ((G + client_text) + W))
        else:
            print ''
    ri = raw_input(((((((((((GR + '\n    [+]') + W) + '    select    ') + G) + 'target    numbers') + W) + '    (') + G) + ('1-%s)' % (str(len(targets)) + W))) + ("    separated    by    commas,    or    '%s':    " % ((G + 'all') + W))))
    if (ri.strip().lower() == 'all'):
        victims = targets[:]
    else:
        for r in ri.split(','):
            r = r.strip()
            if (r.find('-') != (-1)):
                (sx, sy) = r.split('-')
                if (sx.isdigit() and sy.isdigit()):
                    x = int(sx)
                    y = (int(sy) + 1)
                    for v in xrange(x, y):
                        victims.append(targets[(v - 1)])
            elif ((not r.isdigit()) and (r.strip() != '')):
                print (((O + '    [!]') + R) + ('    not    a    number:    %s    ' % ((O + r) + W)))
            elif (r != ''):
                victims.append(targets[(int(r) - 1)])
    if (len(victims) == 0):
        print ((((O + '\n    [!]    ') + R) + 'no    targets    selected.\n') + W)
        self.RUN_CONFIG.exit_gracefully(0)
    print ''
    print ('    [+]    %s%d%s    target%s    selected.' % (G, len(victims), W, ('' if (len(victims) == 1) else 's')))
    return (victims, clients)
