1 from Components.config import config, ConfigYesNo, NoSave, ConfigSubsection, ConfigText, ConfigSelection, ConfigPassword
2 from Components.Console import Console
4 from os import system, path as os_path
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 "[Wlan] killing self.WlanConsole"
50 self.WlanConsole.killAll()
51 self.WlanConsole = None
54 def getDataForInterface(self, callback = None):
55 if len(self.WlanConsole.appContainers) == 0:
56 self.WlanConsole = Console()
57 cmd = "iwconfig " + self.iface
58 self.WlanConsole.ePopen(cmd, self.iwconfigFinished, callback)
60 def iwconfigFinished(self, result, retval, extra_args):
62 data = { 'essid': False, 'frequency': False, 'acesspoint': False, 'bitrate': False, 'encryption': False, 'quality': False, 'signal': False }
64 for line in result.splitlines():
68 ssid = _("No Connection")
70 if "Nickname" in line:
71 tmpssid=(line[line.index('ESSID')+7:line.index('" Nickname')])
73 ssid = _("Hidden networkname")
75 ssid = _("Hidden networkname")
79 tmpssid=(line[line.index('ESSID')+7:len(line)-1])
81 ssid = _("Hidden networkname")
83 ssid = _("Hidden networkname")
88 if 'Frequency' in line:
89 frequency = line[line.index('Frequency')+10 :line.index(' GHz')]
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]
95 data['acesspoint'] = ap
96 if "Bit Rate" in line:
97 br = line[line.index('Bit Rate')+9 :line.index(' Mb/s')]
100 if 'Encryption key' in line:
104 enc = line[line.index('Encryption key')+15 :line.index(' Security')]
106 data['encryption'] = _("Enabled")
107 if 'Quality' in line:
109 qual = line[line.index('Quality')+8:line.index('/100')]
111 qual = line[line.index('Quality')+8:line.index('Sig')]
113 data['quality'] = qual
114 if 'Signal level' in line:
115 signal = line[line.index('Signal level')+13 :line.index(' dBm')]
116 if signal is not None:
117 data['signal'] = signal
119 self.wlaniface[self.iface] = data
121 if len(self.WlanConsole.appContainers) == 0:
122 print "[Wlan.py] self.wlaniface after loading:", self.wlaniface
123 if callback is not None:
124 callback(True,self.wlaniface)
126 def getAdapterAttribute(self, attribute):
127 if self.wlaniface.has_key(self.iface):
128 print "[Wlan.py] self.wlaniface.has_key",self.iface
129 if self.wlaniface[self.iface].has_key(attribute):
130 return self.wlaniface[self.iface][attribute]
133 def asciify(self, str):
134 return str.translate(self.asciitrans)
136 def getWirelessInterfaces(self):
137 return getWNICnames()
139 def getNetworkList(self):
140 system("ifconfig "+self.iface+" up")
141 ifobj = Wireless(self.iface) # a Wireless NIC Object
142 #Association mappings
143 #stats, quality, discard, missed_beacon = ifobj.getStatistics()
144 #snr = quality.signallevel - quality.noiselevel
146 scanresults = ifobj.scan()
149 print "[Wlan.py] No Wireless Networks could be found"
151 if scanresults is not None:
153 (num_channels, frequencies) = ifobj.getChannelInfo()
155 for result in scanresults:
158 if result.encode.flags & wififlags.IW_ENCODE_DISABLED > 0:
160 elif result.encode.flags & wififlags.IW_ENCODE_NOKEY > 0:
165 signal = str(result.quality.siglevel-0x100) + " dBm"
166 quality = "%s/%s" % (result.quality.quality,ifobj.getQualityMax().quality)
169 for element in result.custom:
170 element = element.encode()
171 extra.append( strip(self.asciify(element)) )
172 for element in extra:
174 if 'SignalStrength' in element:
175 signal = element[element.index('SignalStrength')+15:element.index(',L')]
176 if 'LinkQuality' in element:
177 quality = element[element.index('LinkQuality')+12:len(element)]
181 'bssid': result.bssid,
182 'channel': frequencies.index(ifobj._formatFrequency(result.frequency.getFrequency())) + 1,
183 'encrypted': encryption,
184 'essid': strip(self.asciify(result.essid)),
186 'maxrate' : ifobj._formatBitrate(result.rate[-1][-1]),
187 'noise' : '',#result.quality.nlevel-0x100,
188 'quality' : str(quality),
189 'signal' : str(signal),
192 #print "GOT APS ENTRY:",aps[bssid]
197 ifobj = Wireless(self.iface)
200 self.channel = str(fq.getChannel(str(ifobj.getFrequency()[0:-3])))
204 'BSSID': str(ifobj.getAPaddr()), #ifobj.getStatistics()
205 'ESSID': str(ifobj.getEssid()),
206 'quality': "%s/%s" % (ifobj.getStatistics()[1].quality,ifobj.getQualityMax().quality),
207 'signal': str(ifobj.getStatistics()[1].siglevel-0x100) + " dBm",
208 'bitrate': str(ifobj.getBitrate()),
209 'channel': str(self.channel),
210 #'channel': str(fq.getChannel(str(ifobj.getFrequency()[0:-3]))),
212 for (key, item) in status.items():
213 if item is "None" or item is "":
214 status[key] = _("N/A")
219 def __init__(self, iface):
223 def writeConfig(self):
224 essid = config.plugins.wlan.essid.value
225 hiddenessid = config.plugins.wlan.hiddenessid.value
226 encrypted = config.plugins.wlan.encryption.enabled.value
227 encryption = config.plugins.wlan.encryption.type.value
228 wepkeytype = config.plugins.wlan.encryption.wepkeytype.value
229 psk = config.plugins.wlan.encryption.psk.value
230 fp = file('/etc/' + self.iface + '_wpa_supplicant.conf', 'w')
231 fp.write('#WPA Supplicant Configuration by enigma2\n')
232 fp.write('ctrl_interface=/var/run/wpa_supplicant\n')
233 fp.write('eapol_version=1\n')
234 fp.write('fast_reauth=1\n')
235 if essid == 'hidden...':
236 fp.write('ap_scan=2\n')
238 fp.write('ap_scan=1\n')
239 fp.write('network={\n')
240 if essid == 'hidden...':
241 fp.write('\tssid="'+hiddenessid+'"\n')
243 fp.write('\tssid="'+essid+'"\n')
244 fp.write('\tscan_ssid=0\n')
246 if encryption in ('WPA', 'WPA2', 'WPA/WPA2'):
247 fp.write('\tkey_mgmt=WPA-PSK\n')
249 if encryption == 'WPA':
250 fp.write('\tproto=WPA\n')
251 fp.write('\tpairwise=TKIP\n')
252 fp.write('\tgroup=TKIP\n')
253 elif encryption == 'WPA2':
254 fp.write('\tproto=RSN\n')
255 fp.write('\tpairwise=CCMP\n')
256 fp.write('\tgroup=CCMP\n')
258 fp.write('\tproto=WPA RSN\n')
259 fp.write('\tpairwise=CCMP TKIP\n')
260 fp.write('\tgroup=CCMP TKIP\n')
261 fp.write('\tpsk="'+psk+'"\n')
262 elif encryption == 'WEP':
263 fp.write('\tkey_mgmt=NONE\n')
264 if wepkeytype == 'ASCII':
265 fp.write('\twep_key0="'+psk+'"\n')
267 fp.write('\twep_key0='+psk+'\n')
269 fp.write('\tkey_mgmt=NONE\n')
273 system('cat /etc/' + self.iface + '_wpa_supplicant.conf')
275 def loadConfig(self):
276 configfile = '/etc/wpa_supplicant.conf'
277 if os_path.isfile('/etc/' + self.iface + '_wpa_supplicant.conf'):
278 configfile = '/etc/' + self.iface + '_wpa_supplicant.conf'
279 print "[wpaSupplicant] using configfile:",configfile
281 #parse the wpasupplicant configfile
282 fp = file(configfile, 'r')
283 supplicant = fp.readlines()
289 split = s.strip().split('=',1)
290 if split[0] == 'ap_scan':
291 print "[Wlan.py] Got Hidden SSID Scan Value "+split[1]
297 elif split[0] == 'ssid':
298 print "[Wlan.py] Got SSID "+split[1][1:-1]
299 essid = split[1][1:-1]
301 elif split[0] == 'proto':
302 config.plugins.wlan.encryption.enabled.value = True
303 if split[1] == 'WPA' :
305 if split[1] == 'RSN':
307 if split[1] in ('WPA RSN', 'WPA WPA2'):
309 config.plugins.wlan.encryption.type.value = mode
310 print "[Wlan.py] Got Encryption: "+mode
312 elif split[0] == 'wep_key0':
313 config.plugins.wlan.encryption.enabled.value = True
314 config.plugins.wlan.encryption.type.value = 'WEP'
315 if split[1].startswith('"') and split[1].endswith('"'):
316 config.plugins.wlan.encryption.wepkeytype.value = 'ASCII'
317 config.plugins.wlan.encryption.psk.value = split[1][1:-1]
319 config.plugins.wlan.encryption.wepkeytype.value = 'HEX'
320 config.plugins.wlan.encryption.psk.value = split[1]
322 elif split[0] == 'psk':
323 config.plugins.wlan.encryption.psk.value = split[1][1:-1]
328 config.plugins.wlan.hiddenessid.value = essid
329 config.plugins.wlan.essid.value = 'hidden...'
331 config.plugins.wlan.hiddenessid.value = essid
332 config.plugins.wlan.essid.value = essid
334 'hiddenessid': config.plugins.wlan.hiddenessid.value,
335 'ssid': config.plugins.wlan.essid.value,
336 'encryption': config.plugins.wlan.encryption.enabled.value,
337 'encryption_type': config.plugins.wlan.encryption.type.value,
338 'encryption_wepkeytype': config.plugins.wlan.encryption.wepkeytype.value,
339 'key': config.plugins.wlan.encryption.psk.value,
342 for (key, item) in wsconfig.items():
343 if item is "None" or item is "":
344 if key == 'hiddenessid':
345 wsconfig['hiddenessid'] = "home"
347 wsconfig['ssid'] = "home"
348 if key == 'encryption':
349 wsconfig['encryption'] = True
350 if key == 'encryption':
351 wsconfig['encryption_type'] = "WPA/WPA2"
352 if key == 'encryption':
353 wsconfig['encryption_wepkeytype'] = "ASCII"
354 if key == 'encryption':
355 wsconfig['key'] = "mysecurewlan"
357 print "[Wlan.py] Error parsing ",configfile
359 'hiddenessid': "home",
362 'encryption_type': "WPA/WPA2",
363 'encryption_wepkeytype': "ASCII",
364 'key': "mysecurewlan",
366 print "[Wlan.py] WS-CONFIG-->",wsconfig
373 self.backupwlaniface = {}
374 self.WlanConsole = Console()
376 def stopWlanConsole(self):
377 if self.WlanConsole is not None:
378 print "[iStatus] killing self.WlanConsole"
379 self.WlanConsole.killAll()
380 self.WlanConsole = None
382 def getDataForInterface(self, iface, callback = None):
383 self.WlanConsole = Console()
384 cmd = "iwconfig " + iface
385 self.WlanConsole.ePopen(cmd, self.iwconfigFinished, [iface, callback])
387 def iwconfigFinished(self, result, retval, extra_args):
388 (iface, callback) = extra_args
389 data = { 'essid': False, 'frequency': False, 'acesspoint': False, 'bitrate': False, 'encryption': False, 'quality': False, 'signal': False }
390 for line in result.splitlines():
393 if "off/any" in line:
394 ssid = _("No Connection")
396 if "Nickname" in line:
397 tmpssid=(line[line.index('ESSID')+7:line.index('" Nickname')])
399 ssid = _("Hidden networkname")
401 ssid = _("Hidden networkname")
405 tmpssid=(line[line.index('ESSID')+7:len(line)-1])
407 ssid = _("Hidden networkname")
409 ssid = _("Hidden networkname")
414 if 'Frequency' in line:
415 frequency = line[line.index('Frequency')+10 :line.index(' GHz')]
416 if frequency is not None:
417 data['frequency'] = frequency
418 if "Access Point" in line:
419 ap=line[line.index('Access Point')+14:len(line)]
421 data['acesspoint'] = ap
422 if ap == "Not-Associated":
423 data['essid'] = _("No Connection")
424 if "Bit Rate" in line:
426 br = line[line.index('Bit Rate')+9 :line.index(' kb/s')]
428 br = _("Unsupported")
432 br = line[line.index('Bit Rate')+9 :line.index(' Mb/s')] + " Mb/s"
435 if 'Encryption key' in line:
437 if data['acesspoint'] is not "Not-Associated":
438 enc = _("Unsupported")
441 elif "Security" in line:
442 enc = line[line.index('Encryption key')+15 :line.index(' Security')]
446 enc = line[line.index('Encryption key')+15 :len(line)]
450 data['encryption'] = enc
451 if 'Quality' in line:
453 qual = line[line.index('Quality')+8:line.index(' Signal')]
455 qual = line[line.index('Quality')+8:line.index('Sig')]
457 data['quality'] = qual
458 if 'Signal level' in line:
460 signal = line[line.index('Signal level')+13 :line.index(' dBm')]
464 signal = line[line.index('Signal level')+13:line.index(' Noise')]
466 signal = line[line.index('Signal level')+13:len(line)]
469 signal = line[line.index('Signal level')+13:line.index(' Noise')]
471 signal = line[line.index('Signal level')+13:len(line)]
472 if signal is not None:
473 data['signal'] = signal
475 self.wlaniface[iface] = data
476 self.backupwlaniface = self.wlaniface
478 if self.WlanConsole is not None:
479 if len(self.WlanConsole.appContainers) == 0:
480 print "[Wlan.py] self.wlaniface after loading:", self.wlaniface
481 if callback is not None:
482 callback(True,self.wlaniface)
484 def getAdapterAttribute(self, iface, attribute):
486 if self.wlaniface.has_key(self.iface):
487 if self.wlaniface[self.iface].has_key(attribute):
488 return self.wlaniface[self.iface][attribute]