1 from Components.config import config, ConfigYesNo, NoSave, ConfigSubsection, ConfigText, ConfigSelection, ConfigPassword
2 from Components.Console import Console
5 from string import maketrans, strip
8 from re import compile as re_compile, search as re_search
9 from pythonwifi.iwlibs import getNICnames, Wireless, Iwfreq, getWNICnames
10 from pythonwifi import flags as wififlags
16 list.append("WPA/WPA2")
19 weplist.append("ASCII")
22 config.plugins.wlan = ConfigSubsection()
23 config.plugins.wlan.essid = NoSave(ConfigText(default = "home", fixed_size = False))
24 config.plugins.wlan.hiddenessid = NoSave(ConfigText(default = "home", fixed_size = False))
26 config.plugins.wlan.encryption = ConfigSubsection()
27 config.plugins.wlan.encryption.enabled = NoSave(ConfigYesNo(default = True))
28 config.plugins.wlan.encryption.type = NoSave(ConfigSelection(list, default = "WPA/WPA2"))
29 config.plugins.wlan.encryption.wepkeytype = NoSave(ConfigSelection(weplist, default = "ASCII"))
30 config.plugins.wlan.encryption.psk = NoSave(ConfigPassword(default = "mysecurewlan", fixed_size = False))
33 def __init__(self, iface):
35 for i in range(0, 255):
44 self.WlanConsole = Console()
45 self.asciitrans = maketrans(a, b)
47 def stopWlanConsole(self):
48 if self.WlanConsole is not None:
49 print "killing self.WlanConsole"
50 self.WlanConsole = None
53 def getDataForInterface(self, callback = None):
54 #get ip out of ip addr, as avahi sometimes overrides it in ifconfig.
55 print "self.iface im getDataForInterface",self.iface
56 if len(self.WlanConsole.appContainers) == 0:
57 self.WlanConsole = Console()
58 cmd = "iwconfig " + self.iface
59 self.WlanConsole.ePopen(cmd, self.iwconfigFinished, callback)
61 def iwconfigFinished(self, result, retval, extra_args):
62 print "self.iface im iwconfigFinished",self.iface
64 data = { 'essid': False, 'frequency': False, 'acesspoint': False, 'bitrate': False, 'encryption': False, 'quality': False, 'signal': False }
66 for line in result.splitlines():
70 ssid = _("No Connection")
72 if "Nickname" in line:
73 tmpssid=(line[line.index('ESSID')+7:line.index('" Nickname')])
75 ssid = _("Hidden networkname")
77 ssid = _("Hidden networkname")
81 tmpssid=(line[line.index('ESSID')+7:len(line)-1])
83 ssid = _("Hidden networkname")
85 ssid = _("Hidden networkname")
91 if 'Frequency' in line:
92 frequency = line[line.index('Frequency')+10 :line.index(' GHz')]
93 if frequency is not None:
94 data['frequency'] = frequency
95 if "Access Point" in line:
96 ap=line[line.index('Access Point')+14:len(line)-1]
98 data['acesspoint'] = ap
99 if "Bit Rate" in line:
100 br = line[line.index('Bit Rate')+9 :line.index(' Mb/s')]
103 if 'Encryption key' in line:
107 enc = line[line.index('Encryption key')+15 :line.index(' Security')]
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')]
116 data['quality'] = qual
117 if 'Signal level' in line:
118 signal = line[line.index('Signal level')+13 :line.index(' dBm')]
119 if signal is not None:
120 data['signal'] = signal
122 self.wlaniface[self.iface] = data
124 if len(self.WlanConsole.appContainers) == 0:
125 print "self.wlaniface after loading:", self.wlaniface
126 self.WlanConsole = None
127 if callback is not None:
128 callback(True,self.wlaniface)
130 def getAdapterAttribute(self, attribute):
131 if self.wlaniface.has_key(self.iface):
132 print "self.wlaniface.has_key",self.iface
133 if self.wlaniface[self.iface].has_key(attribute):
134 return self.wlaniface[self.iface][attribute]
137 def asciify(self, str):
138 return str.translate(self.asciitrans)
141 def getWirelessInterfaces(self):
142 device = re_compile('[a-z]{2,}[0-9]*:')
145 fp = open('/proc/net/wireless', 'r')
148 # append matching pattern, without the trailing colon
149 ifnames.append(device.search(line).group()[:-1])
150 except AttributeError:
155 def getNetworkList(self):
156 system("ifconfig "+self.iface+" up")
157 ifobj = Wireless(self.iface) # a Wireless NIC Object
159 #Association mappings
160 #stats, quality, discard, missed_beacon = ifobj.getStatistics()
161 #snr = quality.signallevel - quality.noiselevel
164 scanresults = ifobj.scan()
167 print "[Wlan.py] No Wireless Networks could be found"
169 if scanresults is not None:
171 (num_channels, frequencies) = ifobj.getChannelInfo()
173 for result in scanresults:
176 if result.encode.flags & wififlags.IW_ENCODE_DISABLED > 0:
178 elif result.encode.flags & wififlags.IW_ENCODE_NOKEY > 0:
183 signal = str(result.quality.siglevel-0x100) + " dBm"
184 quality = "%s/%s" % (result.quality.quality,ifobj.getQualityMax().quality)
187 for element in result.custom:
188 element = element.encode()
189 extra.append( strip(self.asciify(element)) )
190 for element in extra:
192 if 'SignalStrength' in element:
193 signal = element[element.index('SignalStrength')+15:element.index(',L')]
194 if 'LinkQuality' in element:
195 quality = element[element.index('LinkQuality')+12:len(element)]
199 'bssid': result.bssid,
200 'channel': frequencies.index(ifobj._formatFrequency(result.frequency.getFrequency())) + 1,
201 'encrypted': encryption,
202 'essid': strip(self.asciify(result.essid)),
204 'maxrate' : ifobj._formatBitrate(result.rate[-1][-1]),
205 'noise' : '',#result.quality.nlevel-0x100,
206 'quality' : str(quality),
207 'signal' : str(signal),
210 #print "GOT APS ENTRY:",aps[bssid]
216 ifobj = Wireless(self.iface)
219 self.channel = str(fq.getChannel(str(ifobj.getFrequency()[0:-3])))
223 'BSSID': str(ifobj.getAPaddr()), #ifobj.getStatistics()
224 'ESSID': str(ifobj.getEssid()),
225 'quality': "%s/%s" % (ifobj.getStatistics()[1].quality,ifobj.getQualityMax().quality),
226 'signal': str(ifobj.getStatistics()[1].siglevel-0x100) + " dBm",
227 'bitrate': str(ifobj.getBitrate()),
228 'channel': str(self.channel),
229 #'channel': str(fq.getChannel(str(ifobj.getFrequency()[0:-3]))),
232 for (key, item) in status.items():
233 if item is "None" or item is "":
234 status[key] = _("N/A")
244 def writeConfig(self):
246 essid = config.plugins.wlan.essid.value
247 hiddenessid = config.plugins.wlan.hiddenessid.value
248 encrypted = config.plugins.wlan.encryption.enabled.value
249 encryption = config.plugins.wlan.encryption.type.value
250 wepkeytype = config.plugins.wlan.encryption.wepkeytype.value
251 psk = config.plugins.wlan.encryption.psk.value
252 fp = file('/etc/wpa_supplicant.conf', 'w')
253 fp.write('#WPA Supplicant Configuration by enigma2\n')
254 fp.write('ctrl_interface=/var/run/wpa_supplicant\n')
255 fp.write('eapol_version=1\n')
256 fp.write('fast_reauth=1\n')
257 if essid == 'hidden...':
258 fp.write('ap_scan=2\n')
260 fp.write('ap_scan=1\n')
261 fp.write('network={\n')
262 if essid == 'hidden...':
263 fp.write('\tssid="'+hiddenessid+'"\n')
265 fp.write('\tssid="'+essid+'"\n')
266 fp.write('\tscan_ssid=0\n')
268 if encryption in ('WPA', 'WPA2', 'WPA/WPA2'):
269 fp.write('\tkey_mgmt=WPA-PSK\n')
271 if encryption == 'WPA':
272 fp.write('\tproto=WPA\n')
273 fp.write('\tpairwise=TKIP\n')
274 fp.write('\tgroup=TKIP\n')
275 elif encryption == 'WPA2':
276 fp.write('\tproto=RSN\n')
277 fp.write('\tpairwise=CCMP\n')
278 fp.write('\tgroup=CCMP\n')
280 fp.write('\tproto=WPA RSN\n')
281 fp.write('\tpairwise=CCMP TKIP\n')
282 fp.write('\tgroup=CCMP TKIP\n')
283 fp.write('\tpsk="'+psk+'"\n')
284 elif encryption == 'WEP':
285 fp.write('\tkey_mgmt=NONE\n')
286 if wepkeytype == 'ASCII':
287 fp.write('\twep_key0="'+psk+'"\n')
289 fp.write('\twep_key0='+psk+'\n')
291 fp.write('\tkey_mgmt=NONE\n')
295 system("cat /etc/wpa_supplicant.conf")
297 def loadConfig(self):
299 #parse the wpasupplicant configfile
300 fp = file('/etc/wpa_supplicant.conf', 'r')
301 supplicant = fp.readlines()
307 split = s.strip().split('=',1)
308 if split[0] == 'ap_scan':
309 print "[Wlan.py] Got Hidden SSID Scan Value "+split[1]
315 elif split[0] == 'ssid':
316 print "[Wlan.py] Got SSID "+split[1][1:-1]
317 essid = split[1][1:-1]
319 elif split[0] == 'proto':
320 config.plugins.wlan.encryption.enabled.value = True
321 if split[1] == 'WPA' :
323 if split[1] == 'RSN':
325 if split[1] in ('WPA RSN', 'WPA WPA2'):
328 config.plugins.wlan.encryption.type.value = mode
329 print "[Wlan.py] Got Encryption: "+mode
331 elif split[0] == 'wep_key0':
332 config.plugins.wlan.encryption.enabled.value = True
333 config.plugins.wlan.encryption.type.value = 'WEP'
334 if split[1].startswith('"') and split[1].endswith('"'):
335 config.plugins.wlan.encryption.wepkeytype.value = 'ASCII'
336 config.plugins.wlan.encryption.psk.value = split[1][1:-1]
338 config.plugins.wlan.encryption.wepkeytype.value = 'HEX'
339 config.plugins.wlan.encryption.psk.value = split[1]
341 elif split[0] == 'psk':
342 config.plugins.wlan.encryption.psk.value = split[1][1:-1]
347 config.plugins.wlan.hiddenessid.value = essid
348 config.plugins.wlan.essid.value = 'hidden...'
350 config.plugins.wlan.hiddenessid.value = essid
351 config.plugins.wlan.essid.value = essid
353 'hiddenessid': config.plugins.wlan.hiddenessid.value,
354 'ssid': config.plugins.wlan.essid.value,
355 'encryption': config.plugins.wlan.encryption.enabled.value,
356 'encryption_type': config.plugins.wlan.encryption.type.value,
357 'encryption_wepkeytype': config.plugins.wlan.encryption.wepkeytype.value,
358 'key': config.plugins.wlan.encryption.psk.value,
361 for (key, item) in wsconfig.items():
362 if item is "None" or item is "":
363 if key == 'hiddenessid':
364 wsconfig['hiddenessid'] = "home"
366 wsconfig['ssid'] = "home"
367 if key == 'encryption':
368 wsconfig['encryption'] = True
369 if key == 'encryption':
370 wsconfig['encryption_type'] = "WPA/WPA2"
371 if key == 'encryption':
372 wsconfig['encryption_wepkeytype'] = "ASCII"
373 if key == 'encryption':
374 wsconfig['key'] = "mysecurewlan"
377 print "[Wlan.py] Error parsing /etc/wpa_supplicant.conf"
379 'hiddenessid': "home",
382 'encryption_type': "WPA/WPA2",
383 'encryption_wepkeytype': "ASCII",
384 'key': "mysecurewlan",
386 print "[Wlan.py] WS-CONFIG-->",wsconfig
393 self.backupwlaniface = {}
394 self.WlanConsole = Console()
396 def stopWlanConsole(self):
397 if self.WlanConsole is not None:
398 print "killing self.WlanConsole"
399 self.WlanConsole.killAll()
400 self.WlanConsole = None
402 def getDataForInterface(self, iface, callback = None):
403 self.WlanConsole = Console()
404 cmd = "iwconfig " + iface
405 self.WlanConsole.ePopen(cmd, self.iwconfigFinished, [iface, callback])
407 def iwconfigFinished(self, result, retval, extra_args):
408 (iface, callback) = extra_args
409 data = { 'essid': False, 'frequency': False, 'acesspoint': False, 'bitrate': False, 'encryption': False, 'quality': False, 'signal': False }
410 for line in result.splitlines():
413 if "off/any" in line:
414 ssid = _("No Connection")
416 if "Nickname" in line:
417 tmpssid=(line[line.index('ESSID')+7:line.index('" Nickname')])
419 ssid = _("Hidden networkname")
421 ssid = _("Hidden networkname")
425 tmpssid=(line[line.index('ESSID')+7:len(line)-1])
427 ssid = _("Hidden networkname")
429 ssid = _("Hidden networkname")
434 if 'Frequency' in line:
435 frequency = line[line.index('Frequency')+10 :line.index(' GHz')]
436 if frequency is not None:
437 data['frequency'] = frequency
438 if "Access Point" in line:
439 ap=line[line.index('Access Point')+14:len(line)]
441 data['acesspoint'] = ap
442 if ap == "Not-Associated":
443 data['essid'] = _("No Connection")
444 if "Bit Rate" in line:
446 br = line[line.index('Bit Rate')+9 :line.index(' kb/s')]
448 br = _("Unsupported")
452 br = line[line.index('Bit Rate')+9 :line.index(' Mb/s')] + " Mb/s"
455 if 'Encryption key' in line:
457 if data['acesspoint'] is not "Not-Associated":
458 enc = _("Unsupported")
462 enc = line[line.index('Encryption key')+15 :line.index(' Security')]
466 data['encryption'] = enc
467 if 'Quality' in line:
469 #qual = line[line.index('Quality')+8:line.index('/100')]
470 qual = line[line.index('Quality')+8:line.index(' Signal')]
472 qual = line[line.index('Quality')+8:line.index('Sig')]
474 data['quality'] = qual
475 if 'Signal level' in line:
477 signal = line[line.index('Signal level')+13 :line.index(' dBm')]
481 signal = line[line.index('Signal level')+13:line.index(' Noise')]
483 signal = line[line.index('Signal level')+13:len(line)]
486 signal = line[line.index('Signal level')+13:line.index(' Noise')]
488 signal = line[line.index('Signal level')+13:len(line)]
489 if signal is not None:
490 data['signal'] = signal
492 self.wlaniface[iface] = data
493 self.backupwlaniface = self.wlaniface
495 if self.WlanConsole is not None:
496 if len(self.WlanConsole.appContainers) == 0:
497 print "self.wlaniface after loading:", self.wlaniface
498 if callback is not None:
499 callback(True,self.wlaniface)
501 def getAdapterAttribute(self, iface, attribute):
503 if self.wlaniface.has_key(self.iface):
504 if self.wlaniface[self.iface].has_key(attribute):
505 return self.wlaniface[self.iface][attribute]