#!/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()