def extract_cap_handshakes(basedirs):
    """
    Look for cap files with handshakes,
    and extract them to hccap files.
    Does not return anything.
    """


    ###########################################
    # Look for cap/csv handshakes
    
    
    print ""
    print "*** "*30
    print "*** "*30
    print "*** "*30
    print "*** "*30
    print ""
    print "Beginning search for cap files with handshakes"
    print ""
    
    
    handshakes_found = 0
    
    hccap_files = []
    
    for basedir in basedirs:
    
        if basedir[-1] <> '/':
            basedir += '/'
    
        cap_files=[]
        csv_files=[]
        
        for f in os.listdir(basedir):
            name,ext = os.path.splitext(f)
            if ext==".cap":
                if os.path.isfile(basedir+name+".csv"):
                    cap_files.append(basedir+name+".cap")
                    csv_files.append(basedir+name+".csv")
                else: 
                    cap_files.append(basedir+name+".cap")
    
        print ""
        print "[ ] Looking for cap files with handshakes in",basedir
    
    
        for iic, cap_file in enumerate(cap_files):


            f = open(os.devnull, 'w')
            s = subprocess.Popen([WPACLEAN_BIN, '/tmp/cleaned.cap'] + [cap_file], stdout=f, stderr=f)
            s.wait()
            if os.stat('/tmp/cleaned.cap').st_size == 24:
                print '[-] No WPA/WPA2 handshakes captured from %s'%cap_file
                pass
            
            p = open_offline('/tmp/cleaned.cap')
            # filter beacons
            p.filter = 'link[0] == 0x80'
            
            def gotpacket(d, hdr, data):
                bssid = struct.unpack('6B', data[10:16])
                bssid_str = ':'.join(format(x, '02x') for x in bssid)
    
                d.append(bssid_str)
            
            # Parameters are count, callback, user params
            bssid_list = []
            p.loop(-1, gotpacket, bssid_list)


            ##############################33
            # Our BSSID list from the cap file is a 
            # short but interesting set of BSSIDs
            # whose handshakes have been captured.
            #
            for bssid in bssid_list:
        
                BSSID = bssid.upper()
    
                # this is a new bssid
                print '[+] Handshake found!'
                print '    pcap file = %s'%cap_file
                print '    BSSID = %s'%BSSID


                bssid_file = re.sub(r'\:','_',BSSID)
    

                # check if we have a csv file, 
                # airodump-ng dumps a csv file
                # besside-ng does not 
                if csv_files <> []:

                    csv_file = csv_files[iic]
                
                    # extract bssid/essid information
                    stations_list, clients_list = csv2blob(csv_file)
    
                    # stations
                    stations_head, stations_data = parse_aps(stations_list)
    
                    bssid_ix = stations_head.index("BSSID")
                    essid_ix = stations_head.index("ESSID")

                    essid = ''
    
                    all_bssids = [station[bssid_ix].strip() for station in stations_data]
                    all_essids = [station[essid_ix].strip() for station in stations_data]
    
                    if BSSID in all_bssids:
                        this_ix = all_bssids.index(BSSID)
                        this_essid = all_essids[this_ix].strip()
    
                    try:
                        print '    ESSID = %s'%essid
                    except:
                        print '    ESSID N/A'

            
    
                print "    Extracting BSSID handshakes to hccap file [%s] with Aircrack"%( '/tmp'+bssid_file )
                if not DRYRUN:
                    s = subprocess.call([AIRCRACK_BIN, '-J', '/tmp/'+bssid_file, '-b', BSSID, '/tmp/cleaned.cap'],stdout=f,stdin=f)

                src = "/tmp/"+bssid_file+".hccap"
                dest = basedir+bssid_file+".hccap"

                print "    Moving hccap handshake file to [%s]"%(dest)
                if not DRYRUN:
                    s2 = subprocess.call(["cp","-f",src,dest],stdout=f,stdin=f)

                hccap_files.append(dest)
   
                handshakes_found += 1


    
    print ""
    print "Found %d handshakes."%handshakes_found
    
    print ""
    print "*** "*30
    print "*** "*30
    print "*** "*30
    print "*** "*30
    print ""

    return hccap_files
import sys

try:
    csvfile = sys.argv[1]
except:
    usage()

stations_list, clients_list = csv2blob(csvfile)


#################################
# Data for
# Stations
# (Access Points)
#################################

from parse_aps import parse_aps

parse_aps(stations_list)


#################################
# Data for
# Clients
#
#################################

from parse_clients import parse_clients

parse_clients(clients_list)