Skip to content
This repository has been archived by the owner on Jan 27, 2023. It is now read-only.

Commit

Permalink
Add the n-m tool and call this 0.9.2
Browse files Browse the repository at this point in the history
  • Loading branch information
seveas committed Nov 26, 2011
1 parent 879d1b3 commit 4ce1fce
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,3 +1,5 @@
build
debian
dist dist
docs/_build docs/_build
.*.sw? .*.sw?
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Expand Up @@ -50,7 +50,7 @@
# The short X.Y version. # The short X.Y version.
version = '0.9' version = '0.9'
# The full version, including alpha/beta/rc tags. # The full version, including alpha/beta/rc tags.
release = '0.9.1' release = '0.9.2'


# The language for content autogenerated by Sphinx. Refer to documentation # The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages. # for a list of supported languages.
Expand Down
18 changes: 18 additions & 0 deletions docs/index.rst
Expand Up @@ -9,6 +9,10 @@ calls are forwarded to the correct interface.
I wrote it to reduce a 100-line python script to 50 lines. Not realizing that I wrote it to reduce a 100-line python script to 50 lines. Not realizing that
the library has more lines than the ones I removed. Oh well 😊 the library has more lines than the ones I removed. Oh well 😊


As of version 0.9.2, python-networkmanager also ships a command-line utility
called n-m, which allows you to manipulate NetworkManager's state from the
command line.

:mod:`NetworkManager` -- Easy communication with NetworkManager :mod:`NetworkManager` -- Easy communication with NetworkManager
--------------------------------------------------------------- ---------------------------------------------------------------
.. module:: NetworkManager .. module:: NetworkManager
Expand Down Expand Up @@ -126,3 +130,17 @@ interface.
.. toctree:: .. toctree::
:maxdepth: 2 :maxdepth: 2


The n-m utility
---------------
n-m is a command-line tool to deal with network-manager. It can connect you to
defined networks and disconnect you again.

Usage: [options] action [arguments]

Actions:
list - List all defined and active connections
activate - Activate a connection
deactivate - Deactivate a connection
offline - Deactivate all connections

Suggestions for more functionality for this tool are welcome!
106 changes: 106 additions & 0 deletions n-m
@@ -0,0 +1,106 @@
#!/usr/bin/python
"""
n-m is a command-line tool to deal with network-manager. It can connect you to
defined networks and disconnect you again.
"""

usage = """%prog [options] action [arguments]
Actions:
list - List all defined and active connections
activate - Activate a connection
deactivate - Deactivate a connection
offline - Deactivate all connections"""

import NetworkManager
import optparse
import sys

def main():
p = optparse.OptionParser(usage=usage)
opts, args = p.parse_args()

if not args:
p.print_help()
sys.exit(1)

if args[0] == 'list':
list_()

elif args[0] == 'offline':
offline()

elif len(args) < 2:
p.print_help()
sys.exit(1)

elif args[0] == 'activate':
activate(args[1:])

elif args[0] == 'deactivate':
deactivate(args[1:])

def list_():
active = [x.Connection.GetSettings()['connection']['id']
for x in NetworkManager.NetworkManager.ActiveConnections]
connections = [x.GetSettings()['connection']['id']
for x in NetworkManager.Settings.ListConnections()]
for conn in sorted(connections):
prefix = '* ' if conn in active else ' '
print prefix + conn

def activate(names):
connections = NetworkManager.Settings.ListConnections()
connections = dict([(x.GetSettings()['connection']['id'], x) for x in connections])

if not NetworkManager.NetworkManager.NetworkingEnabled:
NetworkManager.NetworkManager.Enable(True)
for n in names:
if n not in connections:
print >>sys.stderr, "No such connection: %s" % n
sys.exit(1)

print "Activating connection '%s'" % n
conn = connections[n]
ctype = conn.GetSettings()['connection']['type']
if ctype == 'vpn':
for dev in NetworkManager.NetworkManager.GetDevices():
if dev.State == NetworkManager.NM_DEVICE_STATE_ACTIVATED and dev.Managed:
break
else:
print >>sys.stderr, "No active, managed device found"
sys.exit(1)
else:
dtype = {
'802-11-wireless': NetworkManager.NM_DEVICE_TYPE_WIFI,
'802-3-ethernet': NetworkManager.NM_DEVICE_TYPE_ETHERNET,
'gsm': NetworkManager.NM_DEVICE_TYPE_MODEM,
}.get(ctype,ctype)
devices = NetworkManager.NetworkManager.GetDevices()

for dev in devices:
if dev.DeviceType == dtype and dev.State == NetworkManager.NM_DEVICE_STATE_DISCONNECTED:
break
else:
print >>sys.stderr, "No suitable and available %s device found" % ctype
sys.exit(1)

NetworkManager.NetworkManager.ActivateConnection(conn, dev, "/")

def deactivate(names):
active = NetworkManager.NetworkManager.ActiveConnections
active = dict([(x.Connection.GetSettings()['connection']['id'], x) for x in active])

for n in names:
if n not in active:
print >>sys.stderr, "No such connection: %s" % n
sys.exit(1)

print "Deactivating connection '%s'" % n
NetworkManager.NetworkManager.DeactivateConnection(active[n])

def offline():
NetworkManager.NetworkManager.Enable(False)

if __name__ == '__main__':
main()
3 changes: 2 additions & 1 deletion setup.py
Expand Up @@ -3,12 +3,13 @@
from distutils.core import setup from distutils.core import setup


setup(name = "python-networkmanager", setup(name = "python-networkmanager",
version = "0.9.1", version = "0.9.2",
author = "Dennis Kaarsemaker", author = "Dennis Kaarsemaker",
author_email = "dennis@kaarsemaker.net", author_email = "dennis@kaarsemaker.net",
url = "http://github.com/seveas/python-networkmanager", url = "http://github.com/seveas/python-networkmanager",
description = "Easy communication with NetworkManager", description = "Easy communication with NetworkManager",
py_modules = ["NetworkManager"], py_modules = ["NetworkManager"],
scripts = ["n-m"],
classifiers = [ classifiers = [
'Development Status :: 5 - Production/Stable', 'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers', 'Intended Audience :: Developers',
Expand Down

0 comments on commit 4ce1fce

Please sign in to comment.