diff options
-rwxr-xr-x | netctl-menu.py | 81 |
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() |