#! /usr/bin/python # import os, sys, time archivo = file('/var/log/portal','w') # File where every message is gonig to be saved archivo.write('') archivo.close() archivo = file('/var/log/portal','a') def autoportal(): portal=0 contador = 5 i = 1 archivo.write(str(time.localtime())+'Starting...'+"\n") archivo.flush() while (i == 1): if (contador >= 10): # scanning every ten minutes os.popen("iwlist eth0 scan") archivo.write(str(time.localtime())+'Scanning interfaces, refreshing UI \n') archivo.flush() contador = 0 contador = contador + 1 archivo.write(str(time.localtime())+"Am I a Portal? \n") archivo.flush() if (portal == 0): archivo.write(str(time.localtime())+"No, Iīm not \n") archivo.flush() info=os.popen("route | grep eth0 | grep default") temp=info.readline().split() if (len(temp) != 0): #Im not a MPP but I have a gw in eth0 ip=temp[1] check=os.popen("ping -c 5 "+str(ip)) # Checking if gw answers... os.popen("sleep 5") check.readline() temp = check.readline().split() if (len(temp) != 0): temp=temp[0] if (temp != "From"): # gw answered. # We should check if the AP has Internet TengoIpMesh() info = os.popen("ifconfig msh0") info.readline() temp = info.readline().split()[1] if (temp != "addr:"): # Iīm in the mesh? os.popen("service mpp start") # yes archivo.write(str(time.localtime())+"Starting MPP \n") archivo.flush() portal = 1 else: # no archivo.write(str(time.localtime())+"DHCP didnīt give me an IP \n") archivo.flush() else: os.popen("service mpp stop") archivo.write(str(time.localtime())+"stopping MPP, I canīt reach the AP. \n") archivo.flush() else: os.popen("service mpp stop") archivo.write(str(time.localtime())+"Stoppiing MPP, AP didnīt respond. \n") archivo.flush() else: # I donīt have a default gw in eth0 archivo.write(str(time.localtime())+"Am I a client? \n") archivo.flush() info=os.popen("route | grep msh0 | grep default") temp=info.readline().split() if (len(temp) != 0): archivo.write(str(time.localtime())+"Yes, yupi! \n") archivo.flush() else: archivo.write(str(time.localtime())+"Iīm not a portal or client, pff \n") archivo.flush() else: # Iīm already a portal, checking my connection TengoIpMesh() # Just checking contador = contador + 1 info=os.popen("route | grep eth0 | grep default") # Looking for my default route temp=info.readline().split() if (len(temp) != 0): ip=temp[1] # IP of my default route check=os.popen("ping -c 5 "+str(ip)) # ...pinging... os.popen("sleep 5") check.readline() temp=check.readline().split() if (len(temp) != 0): temp=temp[0] if (temp == "From"): # I lost my connection os.popen("service mpp stop") # :( portal = 0 archivo.write(str(time.localtime())+"Stopping MPP, I canīt reach my AP \n") archivo.flush() else: os.popen("service mpp start") archivo.write(str(time.localtime())+"I'm a MPP \n") archivo.flush() os.popen("sleep 60") # Waiting an extra minute contador = contador + 1 else: os.popen("service mpp stop") portal = 0 archivo.write(str(time.localtime())+"Sopping MPP, AP didnīt responde \n") archivo.flush() else: # Just for checking, special case. We shouldnīt be here archivo.write(str(time.localtime())+"Stopping MPP, special case \n") archivo.flush() os.popen("service mpp stop") # :( portal = 0 os.popen("sleep 60") # waitting for a minute def TengoIpMesh(): #configurating Mesh info = os.popen("ifconfig msh0") info.readline() temp = info.readline().split()[1] if (temp == "addr:"): # Is not already configurated os.popen("dhclient msh0") # getting an IP os.popen("killall dhclient") os.popen("iwlist eth0 scan") # scanning, circle sometimes kills eth0 archivo.write(str(time.localtime())+"Refreshing graphic interface \n") archivo.flush() contador = 0 autoportal() # Yoruguas