#!/usr/bin/python2.4 #dotWifi version .01 from __future__ import division import popen2 import re from time import sleep import threading debug=0 mischievous=0 rate=20 def shell(command, all=0): outd, ind, errd=popen2.popen3(command) outd=outd.read() errd=errd.read() if globals()["debug"]==1: print command print outd print errd if all==0: return outd elif all==1: return (outd, errd) elif all==3: if errd!="": return False else: return True else: return (outd, errd) def avalibleNetworks(interface): #print "avalibleNetworks" network=[] data=shell("iwlist "+interface+" scan") #print data if data!="": data=data.split("\n", 1)[1] chopper1=re.compile(" {10}Cell [0-9]{2} - ") chopper2=re.compile(" {5,10}") chopper3=re.compile("[:=]") data=re.split(chopper1, data) i=len(data)-1 while i>=0: data[i]=re.sub(chopper2,"",data[i]) data[i]=data[i].split("\n") ii=len(data[i])-1 cum={} while ii>=0: data[i][ii]=re.split(chopper3, data[i][ii], 1) if len(data[i][ii])>1: name=data[i][ii][0].strip().lower() value=data[i][ii][1].strip() if name=="quality": #print value value=value.split(" ")[0].split("/") cum[name]=(int(value[0])/int(value[1]))%1 #print value[0]+"/"+value[1]+"="+`cum[name]` elif name=="frequency": value=value.split(" (",1) #cum[name]=value[0].strip().lower.split(" ")[0]+G cum[name]=value[0].split(" ")[0] value=value[1].strip()[:-1] #print value value=value.split(" ", 1) cum[value[0].strip().lower()]=value[1].strip().lower() elif name=="essid": cum[name]=value.strip().strip("\"") else: cum[name]=value ii-=1 if cum!={}: cum["interface"]=interface cum["indexed"]=0 network.append(cum) i-=1 else: print "ERROR: Nothing found on "+interface+", perhaps it doesnt support scanning." return network #avalibleNetworks() def interfaces(): interfaces=[] data=shell("iwconfig") chopper1=re.compile("[\n]{2}") chopper2=re.compile("[\s]*") data=re.split(chopper1, data) i=len(data)-1 while i>=0: if(data[i])!="": interfaces.append(re.split(chopper2, data[i].split("\n")[0])[0]) i-=1 return interfaces #interfaces() def allNetworks(): #print "All Networks" all=[] interface=interfaces() i=len(interface)-1 while i>=0: shell("ifconfig "+interface[i]+" up") currNet=avalibleNetworks(interface[i]) ii=len(currNet)-1 while ii>=0: all.append(currNet[ii]) ii-=1 shell("ifconfig "+interface[i]+" down") i-=1 return all #print allNetworks() def sortedNetworks(): #print "sortedNetworks" networks=allNetworks() #print networks count=len(networks) order=[] if count>0: i=count-1 while i>=0: ii=count-1 max=-1 maxIndex=0 while ii>=0: currVal=networks[ii]["quality"] #print currVal if currVal>=max and networks[ii]["indexed"]==0: max=networks[ii]["quality"] maxIndex=ii ii-=1 order.append(networks[maxIndex]) networks[maxIndex]["indexed"]=1 i-=1 #print order return order #sortedNetworks() def getConfig(target=""): if target=="": target="/etc/dotwifi.config" fail=0; try: f=open(target, "r") except: print "Make sure config file /etc/dotwifi.config exists and is redable." fail=1 config=[] if fail!=1: data=f.read() f.close() #print data comments=re.compile("\s*#.*") data=re.sub(comments, "", data) while data.find("\n\n")>-1: data=data.replace("\n\n", "\n") data=data.split("\n;\n") i=0 count=len(data)-1 while i=0: data[i][ii]=data[i][ii].split("=", 1) if len(data[i][ii])==2: cum[data[i][ii][0].strip()]=data[i][ii][1].strip() elif len(data[i][ii])==1 and one==0: if len(data[i][ii][0].strip())==17: #print data[i][ii] #cum["mac"]=data[i][ii][0].strip() cum[data[i][ii][0].strip()]="mac" one=1 ii-=1 if cum!={}: config.append(cum) i+=1 #print config return config #getConfig() def hasEntry(data, field): i=len(data)-1 while i>=0: #print data[i] if(data[i].has_key(field)): break #return i i-=1 return i def bestNetwork(networks, config): #networks=sortedNetworks() #print networks #config=getConfig() #print config #print networks i=0 count=len(networks) if globals()["mischievous"]==0: while i=0: if config[entry]["type"]!="ignore": return (i, entry) break i+=1 else: #print "here" while i=0 or networks[i]["encryption key"]=='off': if config[entry]["type"]!="ignore": return (i, entry) break i+=1 #print i return [-1, -1] #print bestNetwork(sortedNetworks(), getConfig()) def linkQuality(interface): fail=0 try: f=open("/proc/net/wireless", "r") except: print "cant read /proc/net/wireless" fail=1 if fail==0: data=f.read() f.close() print data chopper1=re.compile("\n {1,3}"+interface+": ") chopper2=re.compile(" *") data=re.split(chopper1, data) if len(data)>1: #print "here" data=data[1].split("\n")[0].replace(".", "") data=re.split(chopper2, data) #print data return int(data[1]) else: return 0 return 0 #print linkQuality("eth1") #print linkQuality("ath0") def openRoutine(interface, essid, freq): print "openRoutine: interface:"+interface+" essid:"+essid+" freq:"+freq shell("killall -9 wpa_supplicant") shell("ifdown "+interface) shell("iwconfig "+interface+" mode managed") shell("iwconfig "+interface+" essid "+essid) shell("iwconfig "+interface+" freq "+freq) shell("ifup "+interface) def wepRoutine(interface, essid, wep, freq): print "wepRoutine: interface:"+interface+" essid:"+essid+" freq:"+freq shell("killall -9 wpa_supplicant") shell("ifdown "+interface) shell("iwconfig "+interface+" mode managed") shell("iwconfig "+interface+" essid "+essid) shell("iwconfig "+interface+" key "+wep) shell("iwconfig "+interface+" key on") shell("iwconfig "+interface+" freq "+freq) shell("ifup "+interface) def wpaRoutine(interface, driver, essid, wpa, freq): print "wpaRoutine: interface:"+interface+" driver:"+driver+" essid:"+essid+" freq:"+freq shell("killall -9 wpa_supplicant") shell("ifdown "+interface) shell("iwconfig "+interface+" mode managed") shell("iwconfig "+interface+" essid "+essid) shell("wpa_supplicant -B -D"+driver+" -i"+interface+" -c/etc/wpa_supplicant.conf") shell("iwconfig "+interface+" freq "+freq) shell("ifup "+interface) class wifi(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): while 1: #print "here" networks=sortedNetworks() config=getConfig() print networks best=bestNetwork(networks, config) print config print best if best[0]!=-1: if best[1]!=-1: print networks[best[0]] print config[best[1]] if config[best[1]]["type"]=="wpa": wepRoutine(networks[best[0]]["interface"], config[best[1]]["driver"], config[best[1]]["essid"], config[best[1]]["wpa"], networks[best[0]]["channel"]) while linkQuality(networks[best[0]]["interface"])>0 and globals()["run"]==1: sleep(globals()["rate"]) elif config[best[1]]["type"]=="wep": wepRoutine(networks[best[0]]["interface"], config[best[1]]["essid"], config[best[1]]["wep"], networks[best[0]]["channel"]) while linkQuality(networks[best[0]]["interface"])>0 and globals()["run"]==1: sleep(globals()["rate"]) elif config[best[1]]["type"]=="open": openRoutine(networks[best[0]]["interface"], networks[best[0]]["essid"], networks[best[0]]["channel"]) while linkQuality(networks[best[0]]["interface"])>0 and globals()["run"]==1: sleep(globals()["rate"]) else: print "I dont know what type=\""+config[best[1]]["type"]+"\" means" else: openRoutine(networks[best[0]]["interface"], networks[best[0]]["essid"], networks[best[0]]["channel"]) shell("iwconfig "+networks[best[0]]["interface"]+" essid \"\"") shell("iwconfig "+networks[best[0]]["interface"]+" key off") shell("killall -9 wpa_supplicant") shell("ifdown "+networks[best[0]]["interface"]) if globals()["run"]!=1: break sleep(globals()["rate"]) #wifi() run=1 wireless=wifi() wireless.start() junk=raw_input("Press enter key quit:") run=0 print "Please wait, it might take up to "+`rate`+" seconds to finish"