'Get the wireless interface in monitor mode.
Defaults to only device in monitor mode if found.
Otherwise, enumerates list of possible wifi devices
and asks user to select one to put into monitor mode (if multiple).
Uses airmon-ng to put device in monitor mode if needed.
Returns the name (string) of the interface chosen in monitor mode.'

def get_iface(self):

    if (not self.RUN_CONFIG.PRINTED_SCANNING):
        print (((GR + '    [+]') + W) + '    scanning    for    wireless    devices...')
        self.RUN_CONFIG.PRINTED_SCANNING = True
    proc = Popen(['iwconfig'], stdout=PIPE, stderr=DN)
    iface = ''
    monitors = []
    adapters = []
    for line in proc.communicate()[0].split('\n'):
        if (len(line) == 0):
            continue
        if (ord(line[0]) != 32):
            iface = line[:line.find('    ')]
        if (line.find('Mode:Monitor') != (-1)):
            monitors.append(iface)
        else:
            adapters.append(iface)
    if (self.RUN_CONFIG.WIRELESS_IFACE != ''):
        if monitors.count(self.RUN_CONFIG.WIRELESS_IFACE):
            return self.RUN_CONFIG.WIRELESS_IFACE
        elif (self.RUN_CONFIG.WIRELESS_IFACE in adapters):
            print (((R + '    [!]') + O) + ('    could    not    find    wireless    interface    %s    in    monitor    mode' % (((((R + '"') + R) + self.RUN_CONFIG.WIRELESS_IFACE) + '"') + O)))
            return self.enable_monitor_mode(self.RUN_CONFIG.WIRELESS_IFACE)
        else:
            print (((R + '    [!]') + O) + ('    could    not    find    wireless    interface    %s' % ((((('"' + R) + self.RUN_CONFIG.WIRELESS_IFACE) + O) + '"') + W)))
            self.RUN_CONFIG.exit_gracefully(0)
    if (len(monitors) == 1):
        return monitors[0]
    elif (len(monitors) > 1):
        print ((((((GR + '    [+]') + W) + '    interfaces    in    ') + G) + 'monitor    mode:') + W)
        for (i, monitor) in enumerate(monitors):
            print ('        %s.    %s' % (((G + str((i + 1))) + W), ((G + monitor) + W)))
        ri = raw_input(('%s    [+]%s    select    %snumber%s    of    interface    to    use    for    capturing    (%s1-%d%s):    %s' % (GR, W, G, W, G, len(monitors), W, G)))
        while ((not ri.isdigit()) or (int(ri) < 1) or (int(ri) > len(monitors))):
            ri = raw_input(('%s    [+]%s    select    number    of    interface    to    use    for    capturing    (%s1-%d%s):    %s' % (GR, W, G, len(monitors), W, G)))
        i = int(ri)
        return monitors[(i - 1)]
    proc = Popen(['airmon-ng'], stdout=PIPE, stderr=DN)
    for line in proc.communicate()[0].split('\n'):
        if ((len(line) == 0) or line.startswith('Interface') or line.startswith('PHY')):
            continue
        monitors.append(line)
    if (len(monitors) == 0):
        print ((((R + '    [!]') + O) + '    no    wireless    interfaces    were    found.') + W)
        print ((((R + '    [!]') + O) + '    you    need    to    plug    in    a    wifi    device    or    install    drivers.\n') + W)
        self.RUN_CONFIG.exit_gracefully(0)
    elif ((self.RUN_CONFIG.WIRELESS_IFACE != '') and (monitors.count(self.RUN_CONFIG.WIRELESS_IFACE) > 0)):
        monitor = monitors[0][:monitors[0].find(' DCTB ')]
        return self.enable_monitor_mode(monitor)
    elif (len(monitors) == 1):
        monitor = monitors[0][:monitors[0].find(' DCTB ')]
        if monitor.startswith('phy'):
            monitor = monitors[0].split()[1]
        return self.enable_monitor_mode(monitor)
    print (((GR + '    [+]') + W) + '    available    wireless    devices:')
    for (i, monitor) in enumerate(monitors):
        print ('        %s%d%s.    %s' % (G, (i + 1), W, monitor))
    ri = raw_input((((GR + '    [+]') + W) + ('    select    number    of    device    to    put    into    monitor    mode    (%s1-%d%s):    ' % (G, len(monitors), W))))
    while ((not ri.isdigit()) or (int(ri) < 1) or (int(ri) > len(monitors))):
        ri = raw_input(('    [+]    select    number    of    device    to    put    into    monitor    mode    (%s1-%d%s):    ' % (G, len(monitors), W)))
    i = int(ri)
    monitor = monitors[(i - 1)][:monitors[(i - 1)].find(' DCTB ')]
    return self.enable_monitor_mode(monitor)
