def wait_wpa(finstatus="COMPLETED"): n = 0 ant = "" while n<60: print n status = wpa_status() print status if status != ant: rat.rat_echo("WPA_STATUS = %s" % status) ant = status if status == finstatus: return True elif status == "off" or status == "unknown": return False n+=1 time.sleep(1) return False
def start_wpa(): rat.rat_echo("Iniciando WPA") Popen(["wpa_supplicant","-c/etc/wpa_supplicant.conf","-iwlan0","-B"])
def dhcpd(): rat.rat_echo("DHCP discover...") dhc = Popen(["dhclient","wlan0"],stdout=PIPE,stderr=PIPE) rat.rat_echo(dhc.stdout.read() + dhc.stderr.read())
def stop_wpa(): rat.rat_echo("Parando WPA") Popen(["wpa_cli","terminate"])
if on: f.write("proxy.alu.uma.es:3128") f.close() if __name__ == "__main__": import sys action = sys.argv[1] if action == "uni": proxy(True) stop_wpa() print "==============" start_wpa() print "==============" time.sleep(2) #dat tiempo a wpa_supplicant para iniciar res = wait_wpa() if res: print "==============" dhcpd() else: rat.rat_echo("Error conexion (timeout o apagado inesperado)") print "No se ha podido conectar" elif action == "casa": stop_wpa() proxy(False) elif action == "proxy": proxy(True) elif action == "noproxy": proxy(False)