-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnmkv.py
More file actions
468 lines (389 loc) · 13.6 KB
/
nmkv.py
File metadata and controls
468 lines (389 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout
from functools import partial
from subprocess import Popen, PIPE, call
Builder.load_string('''
<WifiInterface>:
orientation: 'vertical'
padding: '30dp'
spacing: '20dp'
GridLayout:
cols: 3
padding: 20
spacing: 20
size_hint: 1,.3
Button:
text: "Show devices"
on_release: root.show_devices_status()
Button:
text: "Disconnect wifi Network"
on_release: root.disconnect_wifi()
TextInput:
id: password
write_tab: False
hint_text: "Password to Connect Encrypted NetWork"
disabled: True
BoxLayout:
id: label_connections
size_hint: 1, 0.4
orientation: 'vertical'
Label:
size_hint_x: 1
size_hint_y: 0.5
valign: 'middle'
height: '35dp'
text: 'clic Button Show device to show Active connection'
Label:
id: label_wifi_actif
size_hint: (1,0.2)
size_hint_y: None
height: sp(10)
text: 'Wifi enabled: ' + str(root.is_enabled())
BoxLayout:
orientation: 'horizontal'
size_hint_y: 0.3
Button:
id: wifi_button
size_hint_y: None
height: sp(35)
text: 'Enable Wifi / Start Scanning'
on_release: root.start_wifi()
Button:
id: stop_wifi_button
size_hint_y: None
height: sp(35)
disabled: True
text: 'Disable Wifi'
on_release: root.stop_wifi()
BoxLayout:
id: scan_layout
orientation: 'vertical'
Label:
size_hint_x: 1
size_hint_y: None
valign: 'middle'
height: '35dp'
text: 'Scan Results'
''')
class WifiInterface(BoxLayout):
param = {}
names = {}
@property
def interfaces(self):
'''
Get all the available interfaces for WiFi.
.. versionadded:: 1.4.0
Tested with nmcli 1.2.6.
'''
if not self._is_enabled():
self._enable()
# fetch the devices
proc = Popen([
'nmcli', '--terse',
'--fields', 'DEVICE,TYPE',
'device'
], stdout=PIPE)
lines = proc.communicate()[0].decode('utf-8').splitlines()
# filter devices by type
interfaces = []
for line in lines:
# bad escape from nmcli's side :<
line = line.replace('\\:', '$$')
device, dtype = line.split(':')
if dtype != 'wifi':
continue
interfaces.append(device.replace('$$', ':'))
# return wifi interfaces
return interfaces
@property
def device_status(self):
output = Popen(['nmcli', '--terse', '--fields','DEVICE,TYPE,STATE,CONNECTION','device', 'status'],
stdout=PIPE)
lines = output.communicate()[0].decode('utf-8').splitlines()
connections = []
for line in lines:
# bad escape from nmcli's side :<
line = line.replace('\\:', '$$')
device, dtype, state, connection = line.split(':')
ligne_connections = ( device, dtype, state, connection) # a tuple
connections.append(ligne_connections) # remplit la liste avec chaque tuple
return connections
def show_devices_status(self):
connections = self.device_status
stack_connections = self.ids['label_connections']
stack_connections.clear_widgets()
content = ""
for objet in connections:
boxl = BoxLayout(orientation='vertical')
label_connection = Button(
text=str(objet),
size_hint=(1, 1),
height='10dp',
halign= 'left'
)
button_detail = Button(
text="Detail",
height= '10dp',
size_hint_x= 0.2,
#on_release=self.detail()
)
boxl.add_widget(label_connection)
#boxl.add_widget(button_detail)
stack_connections.add_widget(boxl)
def _start_scanning(self, interface=None):
'''
Start scanning for available Wi-Fi networks
for the specified interface.
.. versionadded:: 1.4.0
Tested with nmcli 1.2.6.
'''
if not self._is_enabled():
self.enable_wifi()
if not interface:
interface = self.interfaces[0]
# force rescan for fresh data
call(['nmcli', 'device', 'wifi', 'rescan', 'ifname', interface])
# get properties
fields = [
'IN-USE','SSID', 'BSSID', 'MODE', 'CHAN', 'FREQ',
'BARS', 'RATE', 'SIGNAL', 'SECURITY'
]
# fetch all networks for interface
output = Popen([
'nmcli', '--terse',
'--fields', ','.join(fields),
'device', 'wifi', 'list', 'ifname', interface
], stdout=PIPE).communicate()[0].decode('utf-8')
# parse output
for line in output.splitlines():
line = line.replace('\\:', '$$')
row = {
field: value
for field, value in zip(fields, line.split(':'))
}
row['BSSID'] = row['BSSID'].replace('$$', ':')
self.names[row['SSID']] = row
def _get_network_info(self, name):
'''
Get all the network information by network's name (SSID).
.. versionadded:: 1.4.0
Tested with nmcli 1.2.6.
'''
if not self.names:
self._start_scanning()
ret_list = {}
ret_list['ssid'] = self.names[name]['SSID']
connected = self.names[name]['IN-USE']
if connected == '*' :
ret_list['connected'] = 'Connected'
else:
ret_list['connected'] = 'No'
ret_list['signal'] = self.names[name]['SIGNAL']
bars = len(self.names[name]['BARS'])
ret_list['quality'] = '{}/100'.format(bars / 5.0 * 100)
ret_list['frequency'] = self.names[name]['FREQ']
ret_list['bitrates'] = self.names[name]['RATE']
# wpa1, wpa2, wpa1 wpa2, wep, (none), perhaps something else
security = self.names[name]['SECURITY'].lower()
ret_list['encrypted'] = True
if 'wpa2' and 'wpa1' in security:
# wpa2, wpa2+wpa1
ret_list['encryption_type'] = 'wpa2-wpa1'
elif 'wpa1' and not 'wpa2' in security:
ret_list['encryption_type'] = 'wpa1'
elif 'wpa' in security:
ret_list['encryption_type'] = 'wpa'
elif 'wep' in security:
ret_list['encryption_type'] = 'wep'
elif 'none' in security:
ret_list['encrypted'] = False
ret_list['encryption_type'] = 'none'
else:
ret_list['encryption_type'] = security
ret_list['channel'] = int(self.names[name]['CHAN'])
ret_list['address'] = self.names[name]['BSSID']
ret_list['mode'] = self.names[name]['MODE']
return ret_list
def _get_available_wifi(self):
'''
Return the names of all found networks.
.. versionadded:: 1.4.0
Tested with nmcli 1.2.6.
'''
if not self.names:
self._start_scanning()
return list(self.names.keys())
def _create_popup(self, title, content):
return Popup(
title=title,
content=Label(text=content),
size_hint=(.8, 1),
auto_dismiss=True
)
def start_wifi(self):
wifi_button = self.ids['wifi_button']
wifi_button.text = 'Showing Scan Results'
wifi_button.on_release = self.show_wifi_scans
self.start_scanning()
stop_wifi_button = self.ids['stop_wifi_button']
stop_wifi_button.disabled = False
text_inpt = self.ids['password']
text_inpt.disabled = False
self.ids.label_wifi_actif.text = 'Wifi Enable ' + str(self.is_enabled())
self.show_devices_status()
def stop_wifi(self):
stop_wifi_button = self.ids['stop_wifi_button']
stop_wifi_button.disabled = True
wifi_button = self.ids['wifi_button']
wifi_button.text = 'Enable Wifi'
wifi_button.on_release = self.start_wifi
self.disable_wifi()
self.ids.label_wifi_actif.text = 'Wifi Enable ' + str(self.is_enabled())
self.ids['scan_layout'].clear_widgets()
text_inpt = self.ids['password']
text_inpt.disabled = False
self.show_devices_status()
def start_scanning(self):
self._start_scanning()
def clear_wifi_scans(self):
stack = self.ids['scan_layout']
stack.clear_widgets()
def show_wifi_scans(self):
stack = self.ids['scan_layout']
stack.clear_widgets()
wifi_scans = self.names.keys()
for name in wifi_scans:
content = ""
items = self._get_network_info(name)
if self.names[name]['IN-USE'] == '*' :
bouton_connect = 'connected'
else:
bouton_connect = 'Connect'
for key, value in items.items():
content += "{}: {} \n".format(key, value)
password = self._getpassword(name)
if password != '':
content += 'password : ' + password
popup = self._create_popup(name, content)
boxl = BoxLayout(orientation='horizontal')
button_ssid = Button(
text=name,
size_hint=(1, 1),
height='40dp',
on_release=popup.open,
)
button_connect = Button(
text= bouton_connect,
size_hint_x=.2,
on_release=partial(self.connect, name))
boxl.add_widget(button_ssid)
boxl.add_widget(button_connect)
stack.add_widget(boxl)
def is_enabled(self):
return self._is_enabled()
def _is_enabled(self):
'''
Return the status of WiFi device.
.. versionadded:: 1.4.0
Tested with nmcli 1.2.6.
'''
output = Popen(
["nmcli", "radio", "wifi"],
stdout=PIPE
).communicate()[0].decode('utf-8')
if output.split()[0] == 'enabled':
return True
return False
def enable_wifi(self):
'''
Turn WiFi device on.
'''
call(['nmcli', 'radio', 'wifi', 'on'])
def disable_wifi(self):
'''
Turn WiFi device off.
'''
call(['nmcli', 'radio', 'wifi', 'off'])
def _disconnect_wifi(self, interface=None):
'''
Disconnect all the networks managed by Network manager.
.. versionadded:: 1.2.5
'''
if not interface:
interface = self.interfaces[0]
call(['nmcli', 'dev', 'disconnect', interface])
def disconnect_wifi(self):
self._disconnect_wifi()
self.show_devices_status()
self.clear_wifi_scans()
def connect(self, network_name, instance):
self.param['password'] = self.ids['password'].text
self._connect(network_name, self.param)
self.show_devices_status()
self.clear_wifi_scans()
def _connect(self, network, parameters, interface=None):
'''
Connect a specific interface to a WiFi network.
Expects 2 parameters:
- SSID of the network
- parameters: dict
- password: string or None
'''
self.enable_wifi()
if not interface:
interface = self.interfaces[0]
password = self._getpassword(network)
if password == '':
password = parameters.get('password')
command = [
'nmcli', 'device', 'wifi', 'connect', network,
'ifname', interface
]
if password:
command += ['password', password]
call(command)
def _getpassword(self,name_wifi):
'''
nmcli - -show - secrets - t connection show 'name of wifi network'
'''
#command = [ 'nmcli', '--show-secrets', '--terse', 'connection', 'show', name_wifi]
output = Popen([ 'nmcli', '--show-secrets', '--terse', 'connection', 'show', name_wifi],
stdout=PIPE).communicate()[0].decode('utf-8')
# parse output
for line in output.splitlines():
if '802-11-wireless-security.psk:' in line:
security, password = line.split(':')
return password
return ''
# je ne sais plus d'où cette fonction vient ( à supprimer)
def connect_2_a_tester(self, network, parameters, interface=None):
'''
Expects 2 parameters:
- name/ssid of the network.
- parameters: dict type
- password: string or None
.. versionadded:: 1.2.5
'''
if not interface:
interface = self.interfaces[0]
result = None
try:
self.enable_wifi()
finally:
password = parameters['password']
cell = self.names[network]
result = self.Scheme.for_cell(
interface, network, cell, password
)
return result
class WifiApp(App):
def build(self):
return WifiInterface()
def on_pause(self):
return True
if __name__ == "__main__":
WifiApp().run()