1 from Components.config import config, ConfigYesNo, NoSave, ConfigSubsection, ConfigText, ConfigSelection, ConfigPassword
2 from Components.Console import Console
3 from Components.Network import iNetwork
5 from os import system, path as os_path
6 from string import maketrans, strip
9 from re import compile as re_compile, search as re_search
10 from pythonwifi.iwlibs import getNICnames, Wireless, Iwfreq, getWNICnames
11 from pythonwifi import flags as wififlags
17 list.append("WPA/WPA2")
20 weplist.append("ASCII")
23 config.plugins.wlan = ConfigSubsection()
24 config.plugins.wlan.essid = NoSave(ConfigText(default = "home", fixed_size = False))
25 config.plugins.wlan.hiddenessid = NoSave(ConfigText(default = "home", fixed_size = False))
27 config.plugins.wlan.encryption = ConfigSubsection()
28 config.plugins.wlan.encryption.enabled = NoSave(ConfigYesNo(default = True))
29 config.plugins.wlan.encryption.type = NoSave(ConfigSelection(list, default = "WPA/WPA2"))
30 config.plugins.wlan.encryption.wepkeytype = NoSave(ConfigSelection(weplist, default = "ASCII"))
31 config.plugins.wlan.encryption.psk = NoSave(ConfigPassword(default = "mysecurewlan", fixed_size = False))
33 def getWlanConfigName(iface):
34 return '/etc/wpa_supplicant.' + iface + '.conf'
37 def __init__(self, iface = None):
39 self.oldInterfaceState = None
42 for i in range(0, 255):
49 self.asciitrans = maketrans(a, b)
51 def asciify(self, str):
52 return str.translate(self.asciitrans)
54 def getWirelessInterfaces(self):
57 def setInterface(self, iface = None):
60 def getInterface(self):
63 def getNetworkList(self):
64 if self.oldInterfaceState is None:
65 self.oldInterfaceState = iNetwork.getAdapterAttribute(self.iface, "up")
66 if self.oldInterfaceState is False:
67 if iNetwork.getAdapterAttribute(self.iface, "up") is False:
68 iNetwork.setAdapterAttribute(self.iface, "up", True)
69 system("ifconfig "+self.iface+" up")
71 ifobj = Wireless(self.iface) # a Wireless NIC Object
74 scanresults = ifobj.scan()
77 print "[Wlan.py] No Wireless Networks could be found"
79 if scanresults is not None:
81 (num_channels, frequencies) = ifobj.getChannelInfo()
83 for result in scanresults:
86 if result.encode.flags & wififlags.IW_ENCODE_DISABLED > 0:
88 elif result.encode.flags & wififlags.IW_ENCODE_NOKEY > 0:
93 signal = str(result.quality.siglevel-0x100) + " dBm"
94 quality = "%s/%s" % (result.quality.quality,ifobj.getQualityMax().quality)
97 for element in result.custom:
98 element = element.encode()
99 extra.append( strip(self.asciify(element)) )
100 for element in extra:
101 if 'SignalStrength' in element:
102 signal = element[element.index('SignalStrength')+15:element.index(',L')]
103 if 'LinkQuality' in element:
104 quality = element[element.index('LinkQuality')+12:len(element)]
108 'bssid': result.bssid,
109 'channel': frequencies.index(ifobj._formatFrequency(result.frequency.getFrequency())) + 1,
110 'encrypted': encryption,
111 'essid': strip(self.asciify(result.essid)),
113 'maxrate' : ifobj._formatBitrate(result.rate[-1][-1]),
114 'noise' : '',#result.quality.nlevel-0x100,
115 'quality' : str(quality),
116 'signal' : str(signal),
119 #print "GOT APS ENTRY:",aps[bssid]
123 def stopGetNetworkList(self):
124 if self.oldInterfaceState is not None:
125 if self.oldInterfaceState is False:
126 iNetwork.setAdapterAttribute(self.iface, "up", False)
127 system("ifconfig "+self.iface+" down")
128 self.oldInterfaceState = None
137 def writeConfig(self, iface):
138 essid = config.plugins.wlan.essid.value
139 hiddenessid = config.plugins.wlan.hiddenessid.value
140 encrypted = config.plugins.wlan.encryption.enabled.value
141 encryption = config.plugins.wlan.encryption.type.value
142 wepkeytype = config.plugins.wlan.encryption.wepkeytype.value
143 psk = config.plugins.wlan.encryption.psk.value
144 fp = file(getWlanConfigName(iface), 'w')
145 fp.write('#WPA Supplicant Configuration by enigma2\n')
146 fp.write('ctrl_interface=/var/run/wpa_supplicant\n')
147 fp.write('eapol_version=1\n')
148 fp.write('fast_reauth=1\n')
149 if essid == 'hidden...':
150 fp.write('ap_scan=2\n')
152 fp.write('ap_scan=1\n')
153 fp.write('network={\n')
154 if essid == 'hidden...':
155 fp.write('\tssid="'+hiddenessid+'"\n')
157 fp.write('\tssid="'+essid+'"\n')
158 fp.write('\tscan_ssid=0\n')
160 if encryption in ('WPA', 'WPA2', 'WPA/WPA2'):
161 fp.write('\tkey_mgmt=WPA-PSK\n')
163 if encryption == 'WPA':
164 fp.write('\tproto=WPA\n')
165 fp.write('\tpairwise=TKIP\n')
166 fp.write('\tgroup=TKIP\n')
167 elif encryption == 'WPA2':
168 fp.write('\tproto=RSN\n')
169 fp.write('\tpairwise=CCMP\n')
170 fp.write('\tgroup=CCMP\n')
172 fp.write('\tproto=WPA RSN\n')
173 fp.write('\tpairwise=CCMP TKIP\n')
174 fp.write('\tgroup=CCMP TKIP\n')
175 fp.write('\tpsk="'+psk+'"\n')
176 elif encryption == 'WEP':
177 fp.write('\tkey_mgmt=NONE\n')
178 if wepkeytype == 'ASCII':
179 fp.write('\twep_key0="'+psk+'"\n')
181 fp.write('\twep_key0='+psk+'\n')
183 fp.write('\tkey_mgmt=NONE\n')
187 system('cat ' + getWlanConfigName(iface))
189 def loadConfig(self,iface):
190 configfile = getWlanConfigName(iface)
191 if not os_path.exists(configfile):
192 configfile = '/etc/wpa_supplicant.conf'
194 #parse the wpasupplicant configfile
195 print "[Wlan.py] parsing configfile: ",configfile
196 fp = file(configfile, 'r')
197 supplicant = fp.readlines()
203 split = s.strip().split('=',1)
204 if split[0] == 'ap_scan':
205 print "[Wlan.py] Got Hidden SSID Scan Value ",split[1]
211 elif split[0] == 'ssid':
212 print "[Wlan.py] Got SSID ",split[1][1:-1]
213 essid = split[1][1:-1]
215 elif split[0] == 'proto':
216 config.plugins.wlan.encryption.enabled.value = True
217 if split[1] == 'WPA' :
219 if split[1] == 'RSN':
221 if split[1] in ('WPA RSN', 'WPA WPA2'):
223 config.plugins.wlan.encryption.type.value = mode
224 print "[Wlan.py] Got Encryption: "+mode
226 elif split[0] == 'wep_key0':
227 config.plugins.wlan.encryption.enabled.value = True
228 config.plugins.wlan.encryption.type.value = 'WEP'
229 if split[1].startswith('"') and split[1].endswith('"'):
230 config.plugins.wlan.encryption.wepkeytype.value = 'ASCII'
231 config.plugins.wlan.encryption.psk.value = split[1][1:-1]
233 config.plugins.wlan.encryption.wepkeytype.value = 'HEX'
234 config.plugins.wlan.encryption.psk.value = split[1]
236 elif split[0] == 'psk':
237 config.plugins.wlan.encryption.psk.value = split[1][1:-1]
242 config.plugins.wlan.hiddenessid.value = essid
243 config.plugins.wlan.essid.value = 'hidden...'
245 config.plugins.wlan.hiddenessid.value = essid
246 config.plugins.wlan.essid.value = essid
248 'hiddenessid': config.plugins.wlan.hiddenessid.value,
249 'ssid': config.plugins.wlan.essid.value,
250 'encryption': config.plugins.wlan.encryption.enabled.value,
251 'encryption_type': config.plugins.wlan.encryption.type.value,
252 'encryption_wepkeytype': config.plugins.wlan.encryption.wepkeytype.value,
253 'key': config.plugins.wlan.encryption.psk.value,
256 for (key, item) in wsconfig.items():
257 if item is "None" or item is "":
258 if key == 'hiddenessid':
259 wsconfig['hiddenessid'] = "home"
261 wsconfig['ssid'] = "home"
262 if key == 'encryption':
263 wsconfig['encryption'] = True
264 if key == 'encryption':
265 wsconfig['encryption_type'] = "WPA/WPA2"
266 if key == 'encryption':
267 wsconfig['encryption_wepkeytype'] = "ASCII"
268 if key == 'encryption':
269 wsconfig['key'] = "mysecurewlan"
271 print "[Wlan.py] Error parsing ",configfile
273 'hiddenessid': "home",
276 'encryption_type': "WPA/WPA2",
277 'encryption_wepkeytype': "ASCII",
278 'key': "mysecurewlan",
280 print "[Wlan.py] WS-CONFIG-->",wsconfig
287 self.backupwlaniface = {}
288 self.WlanConsole = Console()
290 def stopWlanConsole(self):
291 if self.WlanConsole is not None:
292 print "[iStatus] killing self.WlanConsole"
293 self.WlanConsole.killAll()
294 self.WlanConsole = None
296 def getDataForInterface(self, iface, callback = None):
297 self.WlanConsole = Console()
298 cmd = "iwconfig " + iface
299 self.WlanConsole.ePopen(cmd, self.iwconfigFinished, [iface, callback])
301 def iwconfigFinished(self, result, retval, extra_args):
302 (iface, callback) = extra_args
303 data = { 'essid': False, 'frequency': False, 'acesspoint': False, 'bitrate': False, 'encryption': False, 'quality': False, 'signal': False }
304 for line in result.splitlines():
307 if "off/any" in line:
308 ssid = _("No Connection")
310 if "Nickname" in line:
311 tmpssid=(line[line.index('ESSID')+7:line.index('" Nickname')])
313 ssid = _("Hidden networkname")
315 ssid = _("Hidden networkname")
319 tmpssid=(line[line.index('ESSID')+7:len(line)-1])
321 ssid = _("Hidden networkname")
323 ssid = _("Hidden networkname")
328 if 'Frequency' in line:
329 frequency = line[line.index('Frequency')+10 :line.index(' GHz')]
330 if frequency is not None:
331 data['frequency'] = frequency
332 if "Access Point" in line:
333 ap=line[line.index('Access Point')+14:len(line)]
335 data['acesspoint'] = ap
336 if ap == "Not-Associated":
337 data['essid'] = _("No Connection")
338 if "Bit Rate" in line:
340 br = line[line.index('Bit Rate')+9 :line.index(' kb/s')]
342 br = _("Unsupported")
346 br = line[line.index('Bit Rate')+9 :line.index(' Mb/s')] + " Mb/s"
349 if 'Encryption key' in line:
351 if data['acesspoint'] is not "Not-Associated":
352 enc = _("Unsupported")
355 elif "Security" in line:
356 enc = line[line.index('Encryption key')+15 :line.index(' Security')]
360 enc = line[line.index('Encryption key')+15 :len(line)]
364 data['encryption'] = enc
365 if 'Quality' in line:
367 qual = line[line.index('Quality')+8:line.index(' Signal')]
369 qual = line[line.index('Quality')+8:line.index('Sig')]
371 data['quality'] = qual
372 if 'Signal level' in line:
374 signal = line[line.index('Signal level')+13 :line.index(' dBm')]
378 signal = line[line.index('Signal level')+13:line.index(' Noise')]
380 signal = line[line.index('Signal level')+13:len(line)]
383 signal = line[line.index('Signal level')+13:line.index(' Noise')]
385 signal = line[line.index('Signal level')+13:len(line)]
386 if signal is not None:
387 data['signal'] = signal
389 self.wlaniface[iface] = data
390 self.backupwlaniface = self.wlaniface
392 if self.WlanConsole is not None:
393 if len(self.WlanConsole.appContainers) == 0:
394 print "[Wlan.py] self.wlaniface after loading:", self.wlaniface
395 if callback is not None:
396 callback(True,self.wlaniface)
398 def getAdapterAttribute(self, iface, attribute):
400 if self.wlaniface.has_key(self.iface):
401 if self.wlaniface[self.iface].has_key(attribute):
402 return self.wlaniface[self.iface][attribute]