1 #from enigma import eListboxPythonMultiContent, eListbox, gFont, RT_HALIGN_LEFT, RT_HALIGN_RIGHT, RT_HALIGN_CENTER
2 #from Components.MultiContent import MultiContentEntryText
3 #from Components.GUIComponent import GUIComponent
4 #from Components.HTMLComponent import HTMLComponent
5 from Components.config import config, ConfigYesNo, NoSave, ConfigSubsection, ConfigText, ConfigSelection, ConfigPassword
6 from Components.Console import Console
9 from string import maketrans, strip
12 from re import compile as re_compile, search as re_search
13 from iwlibs import getNICnames, Wireless, Iwfreq
19 list.append("WPA/WPA2")
22 weplist.append("ASCII")
25 config.plugins.wlan = ConfigSubsection()
26 config.plugins.wlan.essid = NoSave(ConfigText(default = "home", fixed_size = False))
27 config.plugins.wlan.hiddenessid = NoSave(ConfigText(default = "home", fixed_size = False))
29 config.plugins.wlan.encryption = ConfigSubsection()
30 config.plugins.wlan.encryption.enabled = NoSave(ConfigYesNo(default = True))
31 config.plugins.wlan.encryption.type = NoSave(ConfigSelection(list, default = "WPA/WPA2"))
32 config.plugins.wlan.encryption.wepkeytype = NoSave(ConfigSelection(weplist, default = "ASCII"))
33 config.plugins.wlan.encryption.psk = NoSave(ConfigPassword(default = "mysecurewlan", fixed_size = False))
36 def __init__(self, iface):
38 for i in range(0, 255):
47 self.WlanConsole = Console()
48 self.asciitrans = maketrans(a, b)
50 def stopWlanConsole(self):
51 if self.WlanConsole is not None:
52 print "killing self.WlanConsole"
53 self.WlanConsole = None
56 def getDataForInterface(self, callback = None):
57 #get ip out of ip addr, as avahi sometimes overrides it in ifconfig.
58 print "self.iface im getDataForInterface",self.iface
59 if len(self.WlanConsole.appContainers) == 0:
60 self.WlanConsole = Console()
61 cmd = "iwconfig " + self.iface
62 self.WlanConsole.ePopen(cmd, self.iwconfigFinished, callback)
64 def iwconfigFinished(self, result, retval, extra_args):
65 print "self.iface im iwconfigFinished",self.iface
67 data = { 'essid': False, 'frequency': False, 'acesspoint': False, 'bitrate': False, 'encryption': False, 'quality': False, 'signal': False }
68 #print "result im iwconfigFinished",result
70 for line in result.splitlines():
75 ssid = _("No Connection")
77 tmpssid=(line[line.index('ESSID')+7:len(line)-1])
79 ssid = _("Hidden networkname")
81 ssid = _("Hidden networkname")
87 if 'Frequency' in line:
88 frequency = line[line.index('Frequency')+10 :line.index(' GHz')]
89 #print "Frequency",frequency
90 if frequency is not None:
91 data['frequency'] = frequency
92 if "Access Point" in line:
93 ap=line[line.index('Access Point')+14:len(line)-1]
96 data['acesspoint'] = ap
97 if "Bit Rate" in line:
98 br = line[line.index('Bit Rate')+9 :line.index(' Mb/s')]
102 if 'Encryption key' in line:
106 enc = line[line.index('Encryption key')+15 :line.index(' Security')]
107 #print "Encryption key",enc
109 data['encryption'] = _("Enabled")
110 if 'Quality' in line:
112 qual = line[line.index('Quality')+8:line.index('/100')]
114 qual = line[line.index('Quality')+8:line.index('Sig')]
115 #print "Quality",qual
117 data['quality'] = qual
118 if 'Signal level' in line:
119 signal = line[line.index('Signal level')+14 :line.index(' dBm')]
120 #print "Signal level",signal
121 if signal is not None:
122 data['signal'] = signal
124 self.wlaniface[self.iface] = data
126 if len(self.WlanConsole.appContainers) == 0:
127 print "self.wlaniface after loading:", self.wlaniface
128 self.WlanConsole = None
129 if callback is not None:
130 callback(True,self.wlaniface)
132 def getAdapterAttribute(self, attribute):
133 print "im getAdapterAttribute"
134 if self.wlaniface.has_key(self.iface):
135 print "self.wlaniface.has_key",self.iface
136 if self.wlaniface[self.iface].has_key(attribute):
137 return self.wlaniface[self.iface][attribute]
140 def asciify(self, str):
141 return str.translate(self.asciitrans)
144 def getWirelessInterfaces(self):
147 iwifaces = getNICnames()
149 print "[Wlan.py] No Wireless Networkcards could be found"
154 def getNetworkList(self):
155 system("ifconfig "+self.iface+" up")
156 ifobj = Wireless(self.iface) # a Wireless NIC Object
158 #Association mappings
159 stats, quality, discard, missed_beacon = ifobj.getStatistics()
160 snr = quality.signallevel - quality.noiselevel
163 scanresults = ifobj.scan()
166 print "[Wlan.py] No Wireless Networks could be found"
168 if scanresults is not None:
170 for result in scanresults:
174 encryption = map(lambda x: hex(ord(x)), result.encode)
176 if encryption[-1] == "0x8":
182 for element in result.custom:
183 element = element.encode()
184 extra.append( strip(self.asciify(element)) )
186 if result.quality.sl is 0 and len(extra) > 0:
187 begin = extra[0].find('SignalStrength=')+15
193 if extra[0][begin:end].isdigit():
199 signal = extra[0][begin:end]
200 #print "[Wlan.py] signal is:" + str(signal)
203 signal = str(result.quality.sl)
207 'bssid': result.bssid,
208 'channel': result.frequency.getChannel(result.frequency.getFrequency()),
209 'encrypted': encryption,
210 'essid': strip(self.asciify(result.essid)),
212 'maxrate' : result.rate[-1],
213 'noise' : result.quality.getNoiselevel(),
214 'quality' : str(result.quality.quality),
223 ifobj = Wireless(self.iface)
226 self.channel = str(fq.getChannel(str(ifobj.getFrequency()[0:-3])))
229 #print ifobj.getStatistics()
231 'BSSID': str(ifobj.getAPaddr()),
232 'ESSID': str(ifobj.getEssid()),
233 'quality': str(ifobj.getStatistics()[1].quality),
234 'signal': str(ifobj.getStatistics()[1].sl),
235 'bitrate': str(ifobj.getBitrate()),
236 'channel': str(self.channel),
237 #'channel': str(fq.getChannel(str(ifobj.getFrequency()[0:-3]))),
240 for (key, item) in status.items():
241 if item is "None" or item is "":
242 status[key] = _("N/A")
252 def writeConfig(self):
254 essid = config.plugins.wlan.essid.value
255 hiddenessid = config.plugins.wlan.hiddenessid.value
256 encrypted = config.plugins.wlan.encryption.enabled.value
257 encryption = config.plugins.wlan.encryption.type.value
258 wepkeytype = config.plugins.wlan.encryption.wepkeytype.value
259 psk = config.plugins.wlan.encryption.psk.value
260 fp = file('/etc/wpa_supplicant.conf', 'w')
261 fp.write('#WPA Supplicant Configuration by enigma2\n')
262 fp.write('ctrl_interface=/var/run/wpa_supplicant\n')
263 fp.write('eapol_version=1\n')
264 fp.write('fast_reauth=1\n')
265 if essid == 'hidden...':
266 fp.write('ap_scan=2\n')
268 fp.write('ap_scan=1\n')
269 fp.write('network={\n')
270 if essid == 'hidden...':
271 fp.write('\tssid="'+hiddenessid+'"\n')
273 fp.write('\tssid="'+essid+'"\n')
274 fp.write('\tscan_ssid=0\n')
276 if encryption == 'WPA' or encryption == 'WPA2' or encryption == 'WPA/WPA2' :
277 fp.write('\tkey_mgmt=WPA-PSK\n')
279 if encryption == 'WPA':
280 fp.write('\tproto=WPA\n')
281 fp.write('\tpairwise=TKIP\n')
282 fp.write('\tgroup=TKIP\n')
283 elif encryption == 'WPA2':
284 fp.write('\tproto=WPA RSN\n')
285 fp.write('\tpairwise=CCMP TKIP\n')
286 fp.write('\tgroup=CCMP TKIP\n')
288 fp.write('\tproto=WPA WPA2\n')
289 fp.write('\tpairwise=CCMP\n')
290 fp.write('\tgroup=TKIP\n')
291 fp.write('\tpsk="'+psk+'"\n')
293 elif encryption == 'WEP':
294 fp.write('\tkey_mgmt=NONE\n')
295 if wepkeytype == 'ASCII':
296 fp.write('\twep_key0="'+psk+'"\n')
298 fp.write('\twep_key0='+psk+'\n')
300 fp.write('\tkey_mgmt=NONE\n')
304 system("cat /etc/wpa_supplicant.conf")
306 def loadConfig(self):
308 #parse the wpasupplicant configfile
309 fp = file('/etc/wpa_supplicant.conf', 'r')
310 supplicant = fp.readlines()
316 split = s.strip().split('=',1)
317 if split[0] == 'ap_scan':
318 print "[Wlan.py] Got Hidden SSID Scan Value "+split[1]
324 elif split[0] == 'ssid':
325 print "[Wlan.py] Got SSID "+split[1][1:-1]
326 essid = split[1][1:-1]
328 elif split[0] == 'proto':
329 print "split[1]",split[1]
330 config.plugins.wlan.encryption.enabled.value = True
331 if split[1] == "WPA" :
333 if split[1] == "WPA WPA2" :
335 if split[1] == "WPA RSN" :
337 config.plugins.wlan.encryption.type.value = mode
338 print "[Wlan.py] Got Encryption: "+mode
341 #elif split[0] == 'key_mgmt':
342 # print "split[1]",split[1]
343 # if split[1] == "WPA-PSK" :
344 # config.plugins.wlan.encryption.enabled.value = True
345 # config.plugins.wlan.encryption.type.value = "WPA/WPA2"
346 # print "[Wlan.py] Got Encryption: "+ config.plugins.wlan.encryption.type.value
348 elif split[0] == 'wep_key0':
349 config.plugins.wlan.encryption.enabled.value = True
350 config.plugins.wlan.encryption.type.value = 'WEP'
351 if split[1].startswith('"') and split[1].endswith('"'):
352 config.plugins.wlan.encryption.wepkeytype.value = 'ASCII'
353 config.plugins.wlan.encryption.psk.value = split[1][1:-1]
355 config.plugins.wlan.encryption.wepkeytype.value = 'HEX'
356 config.plugins.wlan.encryption.psk.value = split[1]
357 print "[Wlan.py] Got Encryption: WEP - keytype is: "+config.plugins.wlan.encryption.wepkeytype.value
358 print "[Wlan.py] Got Encryption: WEP - key0 is: "+config.plugins.wlan.encryption.psk.value
360 elif split[0] == 'psk':
361 config.plugins.wlan.encryption.psk.value = split[1][1:-1]
362 print "[Wlan.py] Got PSK: "+split[1][1:-1]
367 config.plugins.wlan.hiddenessid.value = essid
368 config.plugins.wlan.essid.value = 'hidden...'
370 config.plugins.wlan.hiddenessid.value = essid
371 config.plugins.wlan.essid.value = essid
373 'hiddenessid': config.plugins.wlan.hiddenessid.value,
374 'ssid': config.plugins.wlan.essid.value,
375 'encryption': config.plugins.wlan.encryption.enabled.value,
376 'encryption_type': config.plugins.wlan.encryption.type.value,
377 'encryption_wepkeytype': config.plugins.wlan.encryption.wepkeytype.value,
378 'key': config.plugins.wlan.encryption.psk.value,
381 for (key, item) in wsconfig.items():
382 if item is "None" or item is "":
383 if key == 'hiddenessid':
384 wsconfig['hiddenessid'] = "home"
386 wsconfig['ssid'] = "home"
387 if key == 'encryption':
388 wsconfig['encryption'] = True
389 if key == 'encryption':
390 wsconfig['encryption_type'] = "WPA/WPA2"
391 if key == 'encryption':
392 wsconfig['encryption_wepkeytype'] = "ASCII"
393 if key == 'encryption':
394 wsconfig['key'] = "mysecurewlan"
397 print "[Wlan.py] Error parsing /etc/wpa_supplicant.conf"
399 'hiddenessid': "home",
402 'encryption_type': "WPA/WPA2",
403 'encryption_wepkeytype': "ASCII",
404 'key': "mysecurewlan",
406 print "[Wlan.py] WS-CONFIG-->",wsconfig
410 def restart(self, iface):
411 system("start-stop-daemon -K -x /usr/sbin/wpa_supplicant")
412 system("start-stop-daemon -S -x /usr/sbin/wpa_supplicant -- -B -i"+iface+" -c/etc/wpa_supplicant.conf")
417 self.backupwlaniface = {}
418 self.WlanConsole = Console()
420 def stopWlanConsole(self):
421 if self.WlanConsole is not None:
422 print "killing self.WlanConsole"
423 self.WlanConsole = None
425 def getDataForInterface(self, iface, callback = None):
426 self.WlanConsole = Console()
427 cmd = "iwconfig " + iface
428 self.WlanConsole.ePopen(cmd, self.iwconfigFinished, [iface, callback])
430 def iwconfigFinished(self, result, retval, extra_args):
431 (iface, callback) = extra_args
432 data = { 'essid': False, 'frequency': False, 'acesspoint': False, 'bitrate': False, 'encryption': False, 'quality': False, 'signal': False }
433 for line in result.splitlines():
436 if "off/any" in line:
437 ssid = _("No Connection")
439 tmpssid=(line[line.index('ESSID')+7:len(line)-1])
441 ssid = _("Hidden networkname")
443 ssid = _("Hidden networkname")
449 if 'Frequency' in line:
450 frequency = line[line.index('Frequency')+10 :line.index(' GHz')]
451 #print "Frequency",frequency
452 if frequency is not None:
453 data['frequency'] = frequency
454 if "Access Point" in line:
455 ap=line[line.index('Access Point')+14:len(line)]
458 data['acesspoint'] = ap
459 if ap == "Not-Associated":
460 data['essid'] = _("No Connection")
461 if "Bit Rate" in line:
463 br = line[line.index('Bit Rate')+9 :line.index(' kb/s')]
465 br = _("Unsupported")
469 br = line[line.index('Bit Rate')+9 :line.index(' Mb/s')] + " Mb/s"
473 if 'Encryption key' in line:
475 if data['acesspoint'] is not "Not-Associated":
476 enc = _("Unsupported")
480 enc = line[line.index('Encryption key')+15 :line.index(' Security')]
483 #print "Encryption key",enc
485 data['encryption'] = enc
486 if 'Quality' in line:
488 qual = line[line.index('Quality')+8:line.index('/100')]
490 qual = line[line.index('Quality')+8:line.index('Sig')]
491 #print "Quality",qual
493 data['quality'] = qual
494 if 'Signal level' in line:
496 signal = line[line.index('Signal level')+14 :line.index(' dBm')]
499 signal = line[line.index('Signal level')+13:line.index('/100 Noise')]
502 signal = line[line.index('Signal level')+13:line.index(' Noise')]
504 #print "Signal level",signal
505 if signal is not None:
506 data['signal'] = signal
508 self.wlaniface[iface] = data
509 self.backupwlaniface = self.wlaniface
511 if self.WlanConsole is not None:
512 if len(self.WlanConsole.appContainers) == 0:
513 print "self.wlaniface after loading:", self.wlaniface
514 if callback is not None:
515 callback(True,self.wlaniface)
517 def getAdapterAttribute(self, iface, attribute):
518 print "im getAdapterAttribute"
520 if self.wlaniface.has_key(self.iface):
521 print "self.wlaniface.has_key",self.iface
522 if self.wlaniface[self.iface].has_key(attribute):
523 return self.wlaniface[self.iface][attribute]