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
|
#!/usr/bin/env python
import subprocess
import sys
import os
class NetctlMenu():
def __init__(self, iface):
self.iface = iface
self.profiles = []
# TODO prevent shell injection.
cmd = '/usr/bin/netctl list | grep -F -- " ' + iface + '" | sort'
out = subprocess.check_output(cmd, shell=True)
for line in out.decode('utf-8').rstrip().split('\n'):
if line == '':
return
active = False
if line[0] == '*':
active = True
identifier = line[2:]
name = identifier.split(iface + '-', maxsplit=1)[1]
self.profiles.append({
'iface': iface,
'active': active,
'identifier': identifier,
'name': name,
})
def list(self):
for p in self.profiles:
print(p)
def run(self):
import dialog
d = dialog.Dialog()
choices = []
for p in self.profiles:
key = p['name']
value = '<- Active' if p['active'] else ''
choices.append((key, value))
choices.append(('', ''))
choices.append(('Run wifi-menu', ''))
message = 'Profiles available for interface ' + self.iface
res = (d.menu(message, choices=choices, height=0, menu_height=25))
if (res[1] == ''):
return
for i, p in enumerate(self.profiles):
if p['name'] == res[1]:
return self.switch_to(i)
os.execl('/usr/bin/wifi-menu', '/usr/bin/wifi-menu', self.iface)
def switch_to(self, index):
''' Calls /usr/bin/netctl switch-to <profile> '''
identifier = self.profiles[index]['identifier']
cmd = ['/usr/bin/netctl', 'switch-to', identifier]
subprocess.check_call(cmd)
def main():
netctl_menu = NetctlMenu(sys.argv[1])
netctl_menu.run()
if __name__ == '__main__':
main()
|