Maybe is working!

parent 40773897
......@@ -99,6 +99,9 @@ def checkYN(res, default='N'):
return False
def netmaskToBit(nmask):
return str(sum(bin(int(x)).count('1') for x in mask.split('.')))
def validateIP(ip):
if not ip:
return False
......@@ -146,6 +149,7 @@ def read_netconf(net):
def write_netconf(net, nconf):
p="/".join([NETPATH, net])
try:
with open(p, 'w') as f:
for k in nconf.keys():
if k!='saved':
......@@ -163,10 +167,31 @@ def write_netconf(net, nconf):
else:
f.write(k+':'+str(nconf[k])+'\n')
f.close()
sys.exit(0)
except:
return False
return True
WPAHEAD="""# needed for wpa_gui to work
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
# needed to allow wpa_gui to alter the configuration
update_config=1
"""
def create_wpacfile(cfile, selected):
# XXX Not all the world is WPA-PSK compatible. And the rest...
# should we try to create a better config?
#
with open(cfile, 'w') as f:
f.write(WPAHEAD)
f.write("\nnetwork={\n")
f.write(" ssid=\""+selected['Name']+"\"\n")
f.write(" scan_ssid=1\n")
f.write(" key_mgmt=WPA-PSK\n")
f.write(" psk=\""+selected['conf']['psk']+"\"\n")
f.write("}\n")
f.close()
def kill_proc(name, ops=False):
for process in psutil.process_iter():
......@@ -177,10 +202,13 @@ def kill_proc(name, ops=False):
else:
process.kill()
def start_wpa(interface):
def start_wpa(interface, conf=False):
kill_proc("dhclient", interface)
kill_proc("wpa_supplicant", interface)
sh("/sbin/wpa_supplicant -s -B -P /run/wpa_supplicant."+interface+".pid -i wlan0 -D nl80211,wext -C /run/wpa_supplicant")
adds=''
if conf:
adds = ' -c '+conf
sh("/sbin/wpa_supplicant -s -B -P /run/wpa_supplicant."+interface+".pid -i wlan0 -D nl80211,wext -C /run/wpa_supplicant"+adds)
def show_fuck_list(nets):
......@@ -380,6 +408,16 @@ while not EXIT:
elif SM=="connect":
done=False
if selected:
cfile='/tmp/wpa.'+interface+'.conf'
create_wpacfile(cfile, selected)
start_wpa(interface, cfile)
if selected['conf']['dhcp']:
sh("dhclient "+interface)
else:
sh("ip address add "+selected['conf']['ip']+"/"+netmaskToBit(selected['conf']['nmask'])+" dev "+interface)
sh("ip link set "+interface+" up")
if selected['conf']['gw']:
sh("ip route add default via "+selected['conf']['gw']+" dev "+interface)
done=True
else:
print_error("FUCK, Are you asking me to connect but you didn't select successfully any network???")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment