From 0266d9299705def8542a57c5340164c5819980ed Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Fri, 9 Mar 2018 13:31:11 +0100 Subject: first commit usage: ./netctl-menu.sh iface Signed-off-by: Olivier Gayot --- netctl-menu.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100755 netctl-menu.py 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 ''' + + 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() -- cgit v1.2.3