summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlivier Gayot <olivier.gayot@sigexec.com>2018-03-09 13:31:11 +0100
committerOlivier Gayot <olivier.gayot@sigexec.com>2018-03-09 13:31:11 +0100
commit0266d9299705def8542a57c5340164c5819980ed (patch)
tree9602e0656fa4628efb916ac7c01f21d8b3e599c3
first commitHEADmaster
usage: ./netctl-menu.sh iface Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
-rwxr-xr-xnetctl-menu.py81
1 files changed, 81 insertions, 0 deletions
diff --git a/netctl-menu.py b/netctl-menu.py
new file mode 100755
index 0000000..c369126
--- /dev/null
+++ b/netctl-menu.py
@@ -0,0 +1,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()