Skip to content

Instantly share code, notes, and snippets.

@lanzafame
Forked from un-def/nmcli-toggle.py
Last active July 11, 2016 06:37
Show Gist options
  • Save lanzafame/534619e651593ddfdd0cc28efbe94c05 to your computer and use it in GitHub Desktop.
Save lanzafame/534619e651593ddfdd0cc28efbe94c05 to your computer and use it in GitHub Desktop.
Toggle network connection up/down using nmcli
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import subprocess
if len(sys.argv) < 2:
print("Usage: nmcli-toggle.py connection_id")
sys.exit()
conn_id = sys.argv[1]
active = subprocess.check_output(['nmcli', 'connection', 'show', '--active'])
up_down = ('up', 'down')[bytes(conn_id, 'utf-8') in active]
subprocess.call(['nmcli', 'connection', up_down, 'id', conn_id])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment