Skip to content

Instantly share code, notes, and snippets.

@hawkins
Created August 21, 2016 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hawkins/523adbc99fab1e76600f31060ba87d5c to your computer and use it in GitHub Desktop.
Save hawkins/523adbc99fab1e76600f31060ba87d5c to your computer and use it in GitHub Desktop.
Python CLI for managing WiFi connections with NMCLI
#!/usr/bin/python
import os
import click
@click.group()
@click.pass_context
def cli(ctx):
"""The CLI for PyFi"""
pass
@cli.command()
@click.argument('ssid')
@click.argument('pw')
@click.option('--iface', default='wlan0', help='Network interface to use.')
def connect(ssid, pw, iface):
"""Connect to a given network"""
click.echo('Connecting to %s' % ssid)
os.system('nmcli d wifi connect "%s" password %s iface %s' % (ssid, pw, iface))
@cli.command()
@click.argument('iface')
def disconnect(iface):
"""Disconnect from a given interface"""
click.echo('Disconnecting interface %s' % iface)
os.system('nmcli d disconnect iface %s' % iface)
if __name__ == "__main__":
cli()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment