Skip to content

Instantly share code, notes, and snippets.

@aborilov
Created August 25, 2015 12:50
Show Gist options
  • Save aborilov/477e0f5d0173c48ce431 to your computer and use it in GitHub Desktop.
Save aborilov/477e0f5d0173c48ce431 to your computer and use it in GitHub Desktop.
switch NetworkManager profiles
#!/usr/bin/env python2
import os
CHAR = "V "
cmd = 'nmcli -f name,devices con status|tail -n+2'
active_devices = os.popen(cmd).read().split('\n')
name_dev = {}
for name_device in active_devices:
if name_device:
name, device = name_device.split()
name_dev[name] = device
all_cons = os.popen('nmcli -f name con list|tail -n+2').read().split()
active_cons = os.popen('nmcli -f name con status|tail -n+2').read().split()
selected_list = [CHAR + con if con in active_cons else con for con in all_cons]
con = os.popen("echo \"" + "\n".join(selected_list) + "\" | dmenu -l 3").read()
if con.startswith(CHAR):
name = con.split()[1]
os.popen("nmcli con down id {}".format(name))
device = name_dev[name]
os.popen("nmcli dev disconnect iface {}".format(device))
else:
name = con
os.popen("nmcli con up id {}".format(name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment